Python Basics
Python Basics
Agenda
⚫ Why Python?
⚫ What is Python?
⚫ Python Applications
⚫ Python Installation
John please tell me with
It has to be Python, let me
which programming
give you enough reasons
language should I start
to believe that
with?
Different Programming Languages
Why Python?
General Purpose
High Level
Interpreted
Dynamic Type
High Level Programming
Interpreter
An interpreter a translator used to translate just one statement
of the program at a time into machine code.
Input Output
High-Level Machine/
Interpreter
Language Low-level language
Dynamic Typing
⚫ Dynamic typing means that the type of the variable is
determined only during runtime.
⚫ Whereas static typing used by few other languages
requires users to declare the variable type.
Dynamic Typing
• name = “John”
Static Typing
‘‘‘
Bulk Comments
Multiline Comments
‘‘‘
2
Indentation
• In simple terms indentation refers to adding
white space before a statement.
Input Input
>>>a=10
a, b = 100, 200
>>>name=‘Nikitha’
print(a)
>>>salary=200.3
# 100
>>>print (a)
print(b)
>>>print(name)
# 200
>>>print(salary)
Tokens
Keywords
Identifiers
Literals
Operators
Keywords
⚫ Python keywords are special reserved words
⚫ Conveys a special meaning to the compiler/interpreter
⚫ Each keyword have special meaning and special
operation
⚫ NEVER use it as a variable.
#Complex Literal
x = 3.14j
print(a, b, c, d)
print(float_1, float_2)
print(x, x.imag, x.real)
Boolean Literal
• A Boolean literal can have any of the two values
⚪ True
⚪ False x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10
print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
Special Literals
• Python contains one special literal i.e. None. We
use it to specify to that field that is not created.
drink = "Available“
food = None
x=drink
if x == drink:
print(food)
else:
print(drink)
Printing Output
• We use the print() function to output data to
the standard output device (screen).
• Syntax :
objects is the value(s) to be printed.
• Membership Operator
if ( id(a) == id(b) ):
print("a and b have same identity")
else:
print("a and b do not have same identity")
Example-2
a=20
c=20.0 Output:
a and c do not have same identity
if a is c: a and c are equal
print("a and c have same identity")
else:
print("a and c do not have same identity")
if a == c:
print("a and c are equal")
else:
print("a and c are not equal")
Membership Operator
• in and not in are the membership operators in Python.
• In a dictionary we can only test for presence of key, not the value.
Operator Precedence
e = (a + b) * c / d
print ("Value of (a + b) * c / d is ", e)
e=a+b*c/d
print("Value of a + b * c / d is ", e)
e = (a + b) * (c / d)
print("Value of (a + b) * (c / d) is ", e)
e = a + (b * c) / d
print("Value of a + (b * c) / d is ", e)
Operator precedence
Determine the hierarchy of operations and evaluate the
following expression:
i = 2 * 3 // 4 + 4 // 4 + 8 - 2 + 5 // 8
Stepwise evaluation of this expression is shown below:
⮚ i = 2 * 3 // 4 + 4 // 4 + 8 - 2 + 5 // 8
⮚ i = 6 // 4 + 4 // 4 + 8 - 2 + 5 // 8 operation: *
⮚ i = 1 + 4 // 4 + 8 - 2 + 5 // 8 operation: //
⮚ i = 1 + 1+ 8 - 2 + 5 // 8 operation: //
⮚ i = 1 + 1 + 8 - 2 + 0 operation: / /
⮚ i = 2 + 8 - 2 + 0 operation: +
⮚ i = 10 - 2 + 0 operation: +
⮚ i = 8 + 0 operation : -
⮚ i = 8 operation: +
Thank you.