SlideShare una empresa de Scribd logo
1 de 35
Python
Python
 Developed by Guido van Rossum
 Derived from many other languages, including ABC, Modula-3, C, C++, Algol-68,
Smalltalk, and Unix shell and other scripting languages.
 Python is copyrighted. Like Perl, Python source code is now available under the
GNU General Public License (GPL).
Ref: www.tutorialspoint.com Created By : Abishek
Python-Features
 Easy-to-learn
 Easy-to-read
 Easy-to-maintain
 A broad standard library
 Interactive Mode
 Portable
 Extendable
 Provide interface to all major Databases
 Supports GUI applications
 Scalable
Ref: www.tutorialspoint.com Created By : Abishek
Advanced Features
 It supports functional and structured programming methods as well as OOP.
 It can be used as a scripting language or can be compiled to byte-code for
building large applications.
 It provides very high-level dynamic data types and supports dynamic type
checking.
 IT supports automatic garbage collection.
 It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
Ref: www.tutorialspoint.com Created By : Abishek
Python Environment Variables
 PYTHONPATH
 PYTHONSTARTUP
 PYTHONCASEOK
 PYTHONHOME
Ref: www.tutorialspoint.com Created By : Abishek
Python Identifiers
 A Python identifier is a name used to identify a variable, function, class, module or
other object.
 Python does not allow punctuation characters such as @, $, and % within
identifiers.
 Class names start with an uppercase letter. All other identifiers start with a
lowercase letter.
 Starting an identifier with a single leading underscore indicates that the identifier is
private.
 Starting an identifier with two leading underscores indicates a strongly private
identifier.
 If the identifier also ends with two trailing underscores, the identifier is a language-
defined special name.
Ref: www.tutorialspoint.com Created By : Abishek
Reserved Words(Key words)
and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield
Ref: www.tutorialspoint.com Created By : Abishek
Lines and Indentation
 Python provides no braces to indicate blocks of code for class and function
definitions or flow control.
 Blocks of code are denoted by line indentation, which is rigidly enforced.
 The number of spaces in the indentation is variable, but all statements within the
block must be indented the same amount.
Ref: www.tutorialspoint.com Created By : Abishek
Multi-Line Statements
 Statements in Python typically end with a new line
 Python allow the use of the line continuation character () to denote that the line
should continue. For Example :-
 total = item_one + 
item_two + 
item_three
Ref: www.tutorialspoint.com Created By : Abishek
Quotation in Python
 Python accepts single ('), double (") and triple (''' or """) quotes to denote string
literals
 The triple quotes are used to span the string across multiple lines. For example, all
the following are legal −
 word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is made up of multiple lines and
sentences."""
Ref: www.tutorialspoint.com Created By : Abishek
Other Syntax in python
 A hash sign (#) that is not inside a string literal begins a comment.
 A line containing only whitespace, possibly with a comment, is known as a blank
line and Python totally ignores it.
 "nn" is used to create two new lines before displaying the actual line.
 The semicolon ( ; ) allows multiple statements on the single line given that neither
statement starts a new code block.
 A group of individual statements, which make a single code block are
called suites in Python
Ref: www.tutorialspoint.com Created By : Abishek
Assigning Values to Variables
 Python variables do not need explicit declaration to reserve memory space.
 The declaration happens automatically when you assign a value to a variable. The
equal sign (=) is used to assign values to variables.
 Python allows you to assign a single value to several variables simultaneously.
Ref: www.tutorialspoint.com Created By : Abishek
Data Types in Python
 Python has five standard data types
 Numbers:
 var2 = 10
 String
 str = 'Hello World!'
 List
 list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
 Tuple
 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
 Dictionary
 dict = {'name': 'john','code':6734, 'dept': 'sales'}
Ref: www.tutorialspoint.com Created By : Abishek
Data Type Conversion
Function Description
int(x [,base]) Converts x to an integer. base specifies the
base if x is a string.
long(x [,base] ) Converts x to a long integer. base specifies the
base if x is a string.
float(x) Converts x to a floating-point number.
complex(real [,imag]) Creates a complex number.
str(x) Converts object x to a string representation.
repr(x) Converts object x to an expression string.
eval(str) Evaluates a string and returns an object.
Ref: www.tutorialspoint.com Created By : Abishek
Cont.
tuple(s) Converts s to a tuple.
list(s) Converts s to a list.
set(s) Converts s to a set.
dict(d) Creates a dictionary. d must be a sequence of
(key,value) tuples.
frozenset(s) Converts s to a frozen set.
chr(x) Converts an integer to a character.
unichr(x) Converts an integer to a Unicode character.
ord(x) Converts a single character to its integer value.
hex(x) Converts an integer to a hexadecimal string.
oct(x) Converts an integer to an octal string.
Function Description
Ref: www.tutorialspoint.com Created By : Abishek
Types of Operator
 Arithmetic Operators
 Comparison (Relational) Operators
 Assignment Operators
 Logical Operators
 Bitwise Operators
 Membership Operators
 Identity Operators
Ref: www.tutorialspoint.com Created By : Abishek
Decision Making
 If statement
 If-Else statement
 Nested If statements
Ref: www.tutorialspoint.com Created By : Abishek
Python Loops
 While loop
 For loop
 Nested loops
Ref: www.tutorialspoint.com Created By : Abishek
Loop control Statements
 break statement
 continue statement
 pass statement
Ref: www.tutorialspoint.com Created By : Abishek
Python Date & Time
 Time intervals are floating-point numbers in units of seconds. Particular instants in
time are expressed in seconds since 12:00am, January 1, 1970(epoch).
 The function time.time() returns the current system time in ticks since 12:00am,
January 1, 1970(epoch).
 Many of Python's time functions handle time as a tuple of 9 numbers
 Get Current time : time.localtime(time.time())
 Getting formatted time : time.asctime(time.localtime(time.time()))
 Getting calendar for a month : calendar.month(year,month)
Ref: www.tutorialspoint.com Created By : Abishek
Python Functions
 We can define the function to provide required functionality. We have to stick with some
rules and regulations to do this.
 Function blocks begin with the keyword def followed by the function name and parentheses ( ( )
).
 Any input parameters or arguments should be placed within these parentheses. You can also
define parameters inside these parentheses.
 The first statement of a function can be an optional statement - the documentation string of the
function or docstring.
 The code block within every function starts with a colon (:) and is indented.
 The statement return [expression] exits a function, optionally passing back an expression to the
caller. A return statement with no arguments is the same as return None.
Ref: www.tutorialspoint.com Created By : Abishek
Syntax and Example for a function
 Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
 Example:
def printme( str ):
"This prints a passed string into this function"
print str
return
Ref: www.tutorialspoint.com Created By : Abishek
Function Arguments
 You can call a function by using the following types of formal arguments:
 Required arguments : Required arguments are the arguments passed to a function in
correct positional order. Here, the number of arguments in the function call should
match exactly with the function definition.
 Keyword arguments : Keyword arguments are related to the function calls. When you
use keyword arguments in a function call, the caller identifies the arguments by the
parameter name.
 Default arguments : A default argument is an argument that assumes a default value if
a value is not provided in the function call for that argument.
 Variable-length arguments : You may need to process a function for more arguments
than you specified while defining the function. These arguments are called variable-
length arguments and are not named in the function definition, unlike required and
default arguments.
Ref: www.tutorialspoint.com Created By : Abishek
Anonymous Functions
 These functions are called anonymous because they are not declared in the standard
manner by using the def keyword. You can use the lambda keyword to create small
anonymous functions.
 Syntax: lambda [arg1 [,arg2,.....argn]]:expression
 Lambda forms can take any number of arguments but return just one value in the form of an
expression.
 They cannot contain commands or multiple expressions.
 An anonymous function cannot be a direct call to print because lambda requires an expression
 Lambda functions have their own local namespace and cannot access variables other than
those in their parameter list and those in the global namespace.
 Although it appears that lambda's are a one-line version of a function, they are not equivalent
to inline statements in C or C++, whose purpose is by passing function stack allocation during
invocation for performance reasons.
Ref: www.tutorialspoint.com Created By : Abishek
Variables
 Scope of a variable
 Global variables
 Variables that are defined outside a function body have a global scope.
 Global variables can be accessed throughout the program body by all functions.
 Local variables
 Variables that are defined inside a function body have a local scope.
 Local variables can be accessed only inside the function in which they are declared
Ref: www.tutorialspoint.com Created By : Abishek
Python Modules
 A module is a Python object with arbitrarily named attributes that you can bind
and reference.
 A module allows you to logically organize your Python code.
 Grouping related code into a module makes the code easier to understand and
use.
 A module can define functions, classes and variables. A module can also include
runnable code.
 You can use any Python source file as a module by executing an import statement
in some other Python source file. The import has the following syntax:
 import module1[, module2[,... moduleN]
Ref: www.tutorialspoint.com Created By : Abishek
Cont.
 Python's from statement lets you import specific attributes from a module into the
current namespace. The from...import has the following syntax −
 from modname import name1[, name2[, ... nameN]]
 Using ‘import *’ will import all names from a module into the current namespace.
 When you import a module, the Python interpreter searches for the module in the
following sequences −
 The current directory.
 If the module isn't found, Python then searches each directory in the shell variable
PYTHONPATH.
 If all else fails, Python checks the default path. On UNIX, this default path is normally
/usr/local/lib/python/.
Ref: www.tutorialspoint.com Created By : Abishek
Python I/O
 Print statement is the simplest way to produce the output.
 We can pass zero or more expressions separated by commas to print statement.
 Eg: print "Python is really a great language,", "isn't it?“
 Reading Keyboard Input
 Python provides two built-in functions to read a line of text
 raw_input : The raw_input([prompt]) function reads one line from standard input and returns it as
a string
 raw_input([prompt])
 Input : The input([prompt]) function is equivalent to raw_input, except that it assumes the input is
a valid Python expression and returns the evaluated result to you.
 input([prompt])
Ref: www.tutorialspoint.com Created By : Abishek
Opening and Closing Files
 The open Function
 Open() is used to open a file
 This function creates a file object, which would be utilized to call other support methods
associated with it.
 Syntax: file object = open(file_name [, access_mode][, buffering])
 file_name: The file_name argument is a string value that contains the name of the file that you
want to access.
 access_mode: The access_mode determines the mode in which the file has to be opened, i.e.,
read, write, append, etc.
 buffering: If the buffering value is set to 0, no buffering takes place. If the buffering value is 1,
line buffering is performed while accessing a file. If you specify the buffering value as an integer
greater than 1, then buffering action is performed with the indicated buffer size. If negative, the
buffer size is the system default(default behavior).
Ref: www.tutorialspoint.com Created By : Abishek
Cont.
 The close() Method
 The close() method of a file object flushes any unwritten information and closes the file
object, after which no more writing can be done.
 Python automatically closes a file when the reference object of a file is reassigned to
another file.
 It is a good practice to use the close() method to close a file.
 Syntax : fileObject.close();
 The write() Method
 The write() method writes any string to an open file.
 The write() method does not add a newline character ('n') to the end of the string −
 Syntax : fileObject.write(string);
 The read() Method
 The read() method reads a string from an open file.
 Syntax : fileObject.read([count]);
Ref: www.tutorialspoint.com Created By : Abishek
Python Exceptions
 An exception is a Python object that represents an error.
 Exception is an event, which occurs during the execution of a program that disrupts
the normal flow of the program's instructions.
 When a Python script encounters a situation that it cannot cope with, it raises an
exception.
Ref: www.tutorialspoint.com Created By : Abishek
Handling an exception
 Exception handling implemented in Python using try-except method.
 You can defend your program by placing the suspicious code in a try: block.
 After the try: block, include an except: statement, followed by a block of code which handles the problem as
elegantly as possible.
 Syntax:
try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
Ref: www.tutorialspoint.com Created By : Abishek
Cont.
 Here are few important points about the above-mentioned syntax −
 A single try statement can have multiple except statements. This is useful when the try block
contains statements that may throw different types of exceptions.
 You can also provide a generic except clause, which handles any exception.
 After the except clause(s), you can include an else-clause. The code in the else-block executes if
the code in the try: block does not raise an exception.
 The else-block is a good place for code that does not need the try: block's protection.
 The except Clause with No Exceptions
 You can also use the except statement with no exceptions defined as follows −
try:
You do your operations here;
......................
except:
If there is any exception, then execute this block.
......................
else:
If there is no exception then execute this block.
Ref: www.tutorialspoint.com Created By : Abishek
Cont.
 The except Clause with Multiple Exceptions
 You can also use the same except statement to handle multiple exceptions as follows −
try:
You do your operations here;
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
If there is any exception from the given exception list,
then execute this block.
......................
else:
If there is no exception then execute this block.
Ref: www.tutorialspoint.com Created By : Abishek
Cont.
 The try-finally Clause
 You can use a finally: block along with a try: block.
 The finally block is a place to put any code that must execute, whether the try-block
raised an exception or not.
 Syntax:
try:
You do your operations here;
......................
Due to any exception, this may be skipped.
finally:
This would always be executed.
......................
Ref: www.tutorialspoint.com Created By : Abishek

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Advanced c programming in Linux
Advanced c programming in Linux Advanced c programming in Linux
Advanced c programming in Linux
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
C programming notes
C programming notesC programming notes
C programming notes
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
 
Functional programming in C++
Functional programming in C++Functional programming in C++
Functional programming in C++
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
DEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellDEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World Haskell
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
 
Functional Programming in C# and F#
Functional Programming in C# and F#Functional Programming in C# and F#
Functional Programming in C# and F#
 
Introduction to Python Basics
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python Basics
 
Python Session - 4
Python Session - 4Python Session - 4
Python Session - 4
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 

Similar a Python Programming Basics for begginners

12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 

Similar a Python Programming Basics for begginners (20)

Functions in C++
Functions in C++Functions in C++
Functions in C++
 
python and perl
python and perlpython and perl
python and perl
 
presentation_intro_to_python
presentation_intro_to_pythonpresentation_intro_to_python
presentation_intro_to_python
 
presentation_intro_to_python_1462930390_181219.ppt
presentation_intro_to_python_1462930390_181219.pptpresentation_intro_to_python_1462930390_181219.ppt
presentation_intro_to_python_1462930390_181219.ppt
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdf
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
 
Python1
Python1Python1
Python1
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning
 
7986-lect 7.pdf
7986-lect 7.pdf7986-lect 7.pdf
7986-lect 7.pdf
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
 
Gude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerGude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic Server
 
Introduction to R for beginners
Introduction to R for beginnersIntroduction to R for beginners
Introduction to R for beginners
 
Python
PythonPython
Python
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 
Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 

Más de Abishek Purushothaman (7)

Aws solution architect
Aws solution architectAws solution architect
Aws solution architect
 
Machine learning
Machine learningMachine learning
Machine learning
 
Multiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritanceMultiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritance
 
Multiple Choice Questions for Java interfaces and exception handling
Multiple Choice Questions for Java interfaces and exception handlingMultiple Choice Questions for Java interfaces and exception handling
Multiple Choice Questions for Java interfaces and exception handling
 
Mini Project presentation for MCA
Mini Project presentation for MCAMini Project presentation for MCA
Mini Project presentation for MCA
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Exception handling
Exception handlingException handling
Exception handling
 

Último

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 

Último (20)

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 

Python Programming Basics for begginners

  • 2. Python  Developed by Guido van Rossum  Derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, Smalltalk, and Unix shell and other scripting languages.  Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public License (GPL). Ref: www.tutorialspoint.com Created By : Abishek
  • 3. Python-Features  Easy-to-learn  Easy-to-read  Easy-to-maintain  A broad standard library  Interactive Mode  Portable  Extendable  Provide interface to all major Databases  Supports GUI applications  Scalable Ref: www.tutorialspoint.com Created By : Abishek
  • 4. Advanced Features  It supports functional and structured programming methods as well as OOP.  It can be used as a scripting language or can be compiled to byte-code for building large applications.  It provides very high-level dynamic data types and supports dynamic type checking.  IT supports automatic garbage collection.  It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java. Ref: www.tutorialspoint.com Created By : Abishek
  • 5. Python Environment Variables  PYTHONPATH  PYTHONSTARTUP  PYTHONCASEOK  PYTHONHOME Ref: www.tutorialspoint.com Created By : Abishek
  • 6. Python Identifiers  A Python identifier is a name used to identify a variable, function, class, module or other object.  Python does not allow punctuation characters such as @, $, and % within identifiers.  Class names start with an uppercase letter. All other identifiers start with a lowercase letter.  Starting an identifier with a single leading underscore indicates that the identifier is private.  Starting an identifier with two leading underscores indicates a strongly private identifier.  If the identifier also ends with two trailing underscores, the identifier is a language- defined special name. Ref: www.tutorialspoint.com Created By : Abishek
  • 7. Reserved Words(Key words) and exec not assert finally or break for pass class from print continue global raise def if return del import try elif in while else is with except lambda yield Ref: www.tutorialspoint.com Created By : Abishek
  • 8. Lines and Indentation  Python provides no braces to indicate blocks of code for class and function definitions or flow control.  Blocks of code are denoted by line indentation, which is rigidly enforced.  The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. Ref: www.tutorialspoint.com Created By : Abishek
  • 9. Multi-Line Statements  Statements in Python typically end with a new line  Python allow the use of the line continuation character () to denote that the line should continue. For Example :-  total = item_one + item_two + item_three Ref: www.tutorialspoint.com Created By : Abishek
  • 10. Quotation in Python  Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals  The triple quotes are used to span the string across multiple lines. For example, all the following are legal −  word = 'word' sentence = "This is a sentence." paragraph = """This is a paragraph. It is made up of multiple lines and sentences.""" Ref: www.tutorialspoint.com Created By : Abishek
  • 11. Other Syntax in python  A hash sign (#) that is not inside a string literal begins a comment.  A line containing only whitespace, possibly with a comment, is known as a blank line and Python totally ignores it.  "nn" is used to create two new lines before displaying the actual line.  The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block.  A group of individual statements, which make a single code block are called suites in Python Ref: www.tutorialspoint.com Created By : Abishek
  • 12. Assigning Values to Variables  Python variables do not need explicit declaration to reserve memory space.  The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.  Python allows you to assign a single value to several variables simultaneously. Ref: www.tutorialspoint.com Created By : Abishek
  • 13. Data Types in Python  Python has five standard data types  Numbers:  var2 = 10  String  str = 'Hello World!'  List  list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]  Tuple  tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )  Dictionary  dict = {'name': 'john','code':6734, 'dept': 'sales'} Ref: www.tutorialspoint.com Created By : Abishek
  • 14. Data Type Conversion Function Description int(x [,base]) Converts x to an integer. base specifies the base if x is a string. long(x [,base] ) Converts x to a long integer. base specifies the base if x is a string. float(x) Converts x to a floating-point number. complex(real [,imag]) Creates a complex number. str(x) Converts object x to a string representation. repr(x) Converts object x to an expression string. eval(str) Evaluates a string and returns an object. Ref: www.tutorialspoint.com Created By : Abishek
  • 15. Cont. tuple(s) Converts s to a tuple. list(s) Converts s to a list. set(s) Converts s to a set. dict(d) Creates a dictionary. d must be a sequence of (key,value) tuples. frozenset(s) Converts s to a frozen set. chr(x) Converts an integer to a character. unichr(x) Converts an integer to a Unicode character. ord(x) Converts a single character to its integer value. hex(x) Converts an integer to a hexadecimal string. oct(x) Converts an integer to an octal string. Function Description Ref: www.tutorialspoint.com Created By : Abishek
  • 16. Types of Operator  Arithmetic Operators  Comparison (Relational) Operators  Assignment Operators  Logical Operators  Bitwise Operators  Membership Operators  Identity Operators Ref: www.tutorialspoint.com Created By : Abishek
  • 17. Decision Making  If statement  If-Else statement  Nested If statements Ref: www.tutorialspoint.com Created By : Abishek
  • 18. Python Loops  While loop  For loop  Nested loops Ref: www.tutorialspoint.com Created By : Abishek
  • 19. Loop control Statements  break statement  continue statement  pass statement Ref: www.tutorialspoint.com Created By : Abishek
  • 20. Python Date & Time  Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in seconds since 12:00am, January 1, 1970(epoch).  The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970(epoch).  Many of Python's time functions handle time as a tuple of 9 numbers  Get Current time : time.localtime(time.time())  Getting formatted time : time.asctime(time.localtime(time.time()))  Getting calendar for a month : calendar.month(year,month) Ref: www.tutorialspoint.com Created By : Abishek
  • 21. Python Functions  We can define the function to provide required functionality. We have to stick with some rules and regulations to do this.  Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).  Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.  The first statement of a function can be an optional statement - the documentation string of the function or docstring.  The code block within every function starts with a colon (:) and is indented.  The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None. Ref: www.tutorialspoint.com Created By : Abishek
  • 22. Syntax and Example for a function  Syntax def functionname( parameters ): "function_docstring" function_suite return [expression]  Example: def printme( str ): "This prints a passed string into this function" print str return Ref: www.tutorialspoint.com Created By : Abishek
  • 23. Function Arguments  You can call a function by using the following types of formal arguments:  Required arguments : Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition.  Keyword arguments : Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.  Default arguments : A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.  Variable-length arguments : You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable- length arguments and are not named in the function definition, unlike required and default arguments. Ref: www.tutorialspoint.com Created By : Abishek
  • 24. Anonymous Functions  These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions.  Syntax: lambda [arg1 [,arg2,.....argn]]:expression  Lambda forms can take any number of arguments but return just one value in the form of an expression.  They cannot contain commands or multiple expressions.  An anonymous function cannot be a direct call to print because lambda requires an expression  Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.  Although it appears that lambda's are a one-line version of a function, they are not equivalent to inline statements in C or C++, whose purpose is by passing function stack allocation during invocation for performance reasons. Ref: www.tutorialspoint.com Created By : Abishek
  • 25. Variables  Scope of a variable  Global variables  Variables that are defined outside a function body have a global scope.  Global variables can be accessed throughout the program body by all functions.  Local variables  Variables that are defined inside a function body have a local scope.  Local variables can be accessed only inside the function in which they are declared Ref: www.tutorialspoint.com Created By : Abishek
  • 26. Python Modules  A module is a Python object with arbitrarily named attributes that you can bind and reference.  A module allows you to logically organize your Python code.  Grouping related code into a module makes the code easier to understand and use.  A module can define functions, classes and variables. A module can also include runnable code.  You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax:  import module1[, module2[,... moduleN] Ref: www.tutorialspoint.com Created By : Abishek
  • 27. Cont.  Python's from statement lets you import specific attributes from a module into the current namespace. The from...import has the following syntax −  from modname import name1[, name2[, ... nameN]]  Using ‘import *’ will import all names from a module into the current namespace.  When you import a module, the Python interpreter searches for the module in the following sequences −  The current directory.  If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.  If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/. Ref: www.tutorialspoint.com Created By : Abishek
  • 28. Python I/O  Print statement is the simplest way to produce the output.  We can pass zero or more expressions separated by commas to print statement.  Eg: print "Python is really a great language,", "isn't it?“  Reading Keyboard Input  Python provides two built-in functions to read a line of text  raw_input : The raw_input([prompt]) function reads one line from standard input and returns it as a string  raw_input([prompt])  Input : The input([prompt]) function is equivalent to raw_input, except that it assumes the input is a valid Python expression and returns the evaluated result to you.  input([prompt]) Ref: www.tutorialspoint.com Created By : Abishek
  • 29. Opening and Closing Files  The open Function  Open() is used to open a file  This function creates a file object, which would be utilized to call other support methods associated with it.  Syntax: file object = open(file_name [, access_mode][, buffering])  file_name: The file_name argument is a string value that contains the name of the file that you want to access.  access_mode: The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc.  buffering: If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line buffering is performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior). Ref: www.tutorialspoint.com Created By : Abishek
  • 30. Cont.  The close() Method  The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done.  Python automatically closes a file when the reference object of a file is reassigned to another file.  It is a good practice to use the close() method to close a file.  Syntax : fileObject.close();  The write() Method  The write() method writes any string to an open file.  The write() method does not add a newline character ('n') to the end of the string −  Syntax : fileObject.write(string);  The read() Method  The read() method reads a string from an open file.  Syntax : fileObject.read([count]); Ref: www.tutorialspoint.com Created By : Abishek
  • 31. Python Exceptions  An exception is a Python object that represents an error.  Exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions.  When a Python script encounters a situation that it cannot cope with, it raises an exception. Ref: www.tutorialspoint.com Created By : Abishek
  • 32. Handling an exception  Exception handling implemented in Python using try-except method.  You can defend your program by placing the suspicious code in a try: block.  After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.  Syntax: try: You do your operations here; ...................... except ExceptionI: If there is ExceptionI, then execute this block. except ExceptionII: If there is ExceptionII, then execute this block. ...................... else: If there is no exception then execute this block. Ref: www.tutorialspoint.com Created By : Abishek
  • 33. Cont.  Here are few important points about the above-mentioned syntax −  A single try statement can have multiple except statements. This is useful when the try block contains statements that may throw different types of exceptions.  You can also provide a generic except clause, which handles any exception.  After the except clause(s), you can include an else-clause. The code in the else-block executes if the code in the try: block does not raise an exception.  The else-block is a good place for code that does not need the try: block's protection.  The except Clause with No Exceptions  You can also use the except statement with no exceptions defined as follows − try: You do your operations here; ...................... except: If there is any exception, then execute this block. ...................... else: If there is no exception then execute this block. Ref: www.tutorialspoint.com Created By : Abishek
  • 34. Cont.  The except Clause with Multiple Exceptions  You can also use the same except statement to handle multiple exceptions as follows − try: You do your operations here; ...................... except(Exception1[, Exception2[,...ExceptionN]]]): If there is any exception from the given exception list, then execute this block. ...................... else: If there is no exception then execute this block. Ref: www.tutorialspoint.com Created By : Abishek
  • 35. Cont.  The try-finally Clause  You can use a finally: block along with a try: block.  The finally block is a place to put any code that must execute, whether the try-block raised an exception or not.  Syntax: try: You do your operations here; ...................... Due to any exception, this may be skipped. finally: This would always be executed. ...................... Ref: www.tutorialspoint.com Created By : Abishek