Intro To Python
Intro To Python
PYTHON
Program
2. According to purpose
Procedural Programming Language
Functional Programming Language
Scripting Programming Language
Object-oriented Programming Language
Low Level Programming Language
• represented in 0 or 1 forms
• LISP, Python
Scripting Programming Language
• supports scripts
• Python's syntax and dynamic typing with its interpreted nature, make
it an ideal language for scripting and rapid application development
in many areas
About Python
• Expressive Language
Code is easily understandable
• Interpreted Language
Interpreter executes the code line by line at a time
Debugging is easy, suitable for beginners
Features of Python
• Cross-platform Language
Run equally on different platforms such as Windows, Linux, Mac etc.
Portable language
• Open Source
Freely available on www.python.org
• Object-oriented Language
Concept of classes and objects exist
Features of Python
• GUI Programming
Graphical user interfaces can be developed using Python
• Integrated Language
Python can be easily integrated with languages like C, C++,
JAVA etc
What can Python do?
• 3D CAD Applications
e.g., Fandango with full features of CAD
Python Applications
• Enterprise Applications
e.g., OpenErp, Tryton
• Installation on Windows –
Download latest version of Python from www.python.org
• DOS Prompt –
To use Python from the Windows command line, need to
set the PATH variable
Control Panel → System → Advanced →
Environment Variables
Specify value for PATH variable in the System Variables section
Running Python
• There are two ways to get the output, i.e., to write values –
1. Expression Statements
2. print() function
Variables
• Example –
Multi-line statements
• Example –
Multiple Statements on Single Line
• Example –
x = 10; y = 20; print(x);
Quotations
• Python accepts single ('), double (") and triple (''' or """') quotes
to denote string literals, as long as the same type of quote
starts and ends the string.
• The triple quotes can be used to span the string across multiple
lines.
Quotations
• Example –
Multiple Assignments to Variable
• Example –
a=b=c=1
Here, an integer object is created with the value 1,
and all three variables are assigned to the same
memory location
Multiple objects to Multiple variables
• Example –
a, b, c = 1, 2, ‘python’
Here, two integer objects with values 1 and 2 are
assigned to variables a and b, and one string object with
the value ‘python’ is assigned to the variable c.
Multiple Statement Groups as Suites
• Example –
Indentation
• All characters after the # and up to the physical line end are
part of the comment and the Python interpreter ignores them.
• They are -
1. Consecutive single-line comment (#)
2. Using a Multi-line string as a comment ( triple quotes)
(Since python will ignore string literals that are not
assigned to a variable)
Standard Data Types
• Numeric
• String
• Boolean
• List
• Tuple
• Set
• Dictionary
Getting Data Type
• We can get the datatype of any object using the type() function.
• e.g., x = 100
print(type(x))
- will print datatype of x
Numeric Type
1. Integers
Value is represented by int class
Contain positive or negative whole numbers
There is no limit to how long an integer value can be
e.g., x = 12
Numeric Types
2. Float
Value is represented by the float class
Store a real number with a floating-point representation
e.g., x = 43.58
3. Complex
Value is represented by a complex class
Specified as (real part) + (imaginary part)j
e.g., x = 2 + 3j
Strings
• Example –
To print doesn’t, two ways are there –
1. “doesn’t” (using double quotes)
2. ‘doesn\’t’ (using \ )
Strings
• Example –
To print “Yes”, he said, two ways are there –
1. ‘ “Yes”, he said ’ (using single quotes)
2. “ \“Yes \”, he said ” (using \ )
Strings
• Example –
1. ‘ “Isn\’t it,” they said. ’
prints the string as it is with enclosing quotes
‘ “Isn\’t it,” they said. ’
2. print(‘ “Isn\’t it,” they said. ’ )
prints the string without enclosing quotes and
escaped characters
“Isn’t it,” they said.
Strings
• Example –
1. str = ‘ First Line \n Second Line ’
prints str as it is with enclosing quotes and \n
‘ First Line \n Second Line ’
2. print( str )
prints the string without enclosing quotes and
producing new line
First Line
Second Line
Strings
• e.g., print(r‘C:\user\bin’)
Strings
• e.g., x = True
List
• Lists are indexed, so lists can have items with the same value.
e.g., list1 = [“Apple”, “Banana”, “Grapes”]
list2 = [“Apple”, “Banana”, “Grapes”, “Banana”, “Apple”]
Here, list1 and list2 are valid lists.
• Lists are ordered, it means that the items have a defined order
and that order will not change.
• If you add new items to a list, the new items will be placed at
the end of the list.
Tuple
• As there is no index, set cannot have two items with the same
value.
Set
• In case two items with same value are added, duplicate values
are ignored.
• Dictionaries cannot have two items with the same key, i.e., it
does not allow duplicate keys.
Dictionary
• In case two items are added for the same key, newly added
value will be considered and previous key-value pair is ignored
• Python allows for user input, i.e., we can ask the user for input.
• int(x, [base]) -
This function converts x to integer
‘base’ specifies the base if x is string
e.g., x = ‘10010’
ans = int(x, 2)
print(ans)
This will print 18
Data Type Conversion
• float(x) –
This function converts x to a floating-point number.
e.g., x = ‘10010’
ans = float(x)
print(ans)
This will print 10010.0
Data Type Conversion
• ord(x) -
This function converts a single character to its integer value
e.g., x = ‘4’
ans = ord(x)
print(ans)
This will print 52
Data Type Conversion
• hex(x) –
This function converts an integer to a hexadecimal string
e.g., x = 56
ans = hex(x)
print(ans)
This will print 0x38
Data Type Conversion
• oct(x) –
This function converts an integer to an octal string
e.g., x = 56
ans = oct(x)
print(ans)
This will print 0o70
Data Type Conversion
• tuple(x) –
This function converts x to a tuple
e.g., x = ‘python’
ans = tuple(x)
print(ans)
This will print - (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
Data Type Conversion
• set(x) –
This function converts x to a set
e.g., x = ‘sicsr’
ans = set(x)
print(ans)
This will print – {‘c’, ‘i’, ‘s’, ‘r’}
Data Type Conversion
• list(x) –
This function converts x to a list
e.g., x = ‘sicsr’
ans = list(x)
print(ans)
This will print – [‘s’, ‘i’, ‘c’, ‘s’, ‘r’]
Data Type Conversion
• dict(x) –
This function converts tuple x into a dictionary, provided that
tuple x has sequence of (key, value)
e.g., x = ((‘a’, 1),(‘b’, 2),(‘c’, 3))
ans = dict(x)
print(ans)
This will print – {‘a’: 1, ‘b’: 2, ‘c’: 3}
Data Type Conversion
• str(x) –
This function converts integer x to a string representation
e.g., x = 12
ans = str(x)
print(ans)
This will print – 12
Data Type Conversion
• complex(real, [imag]) –
This function converts real number to complex number
ans = complex(3, 4)
print(ans)
This will print – (3 + 4j)
Data Type Conversion
• chr(x) –
This function converts integer x to its corresponding ASCII
character.
e.g., x = 65
ans = chr(x)
print(ans)
This will print – A
Operators
• Arithmetic Operators
• Comparison Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Identity Operators
• Membership Operators
Arithmetic Operators
• Addition(+) –
Adds values on either side of the operator
• Subtraction(-) –
Subtracts right hand operand from left hand operand
• Multiplication (*) –
Multiplies values on either side of the operator
Arithmetic Operators
• Division(/) –
Divides left hand operand by right hand operand
• Modulus(%) –
Divides left hand operand by right hand operand and
returns remainder
Arithmetic Operators
• Exponent (**) –
Performs exponential (power) calculation
• Equal (==) –
Checks if the value of two operands is equal or not
Returns true when the values are equal
• is –
Returns True if both variables are the same object or
point to the same object
• is not –
Returns True if both variables are not the same object
or point to different objects
Identity Operators
• To check the memory location, id() function is used, that returns
a unique id for the specified object.
• All objects in Python has its own unique id, which is assigned to
the object when it is created.
• in –
Returns True if a sequence with the specified value is
present in the object
• not in –
Returns True if a sequence with the specified value is not
present in the object
Operator Precedence