Python Basic and Advanced-Day 3
Python Basic and Advanced-Day 3
Python Basic and Advanced-Day 3
1 Python Basics
o Python – Overview
2 Python Advanced
o Python - Classes/Objects
o Python - Environment Setup
Content
o Python - Basic Syntax o Python - Reg Expressions
//=**=, &=
x = 15
y=4
print('x + y =',x+y)
print('x - y =',x-y)
print('x * y =',x*y)
print('x / y =',x/y)
print('x // y =',x//y)
print('x ** y =',x**y)
print('x % y =',x%y)
Python Basics
Operators in Python- Comparison Operator
These operators compare the values on either sides of them and decide the relation among them. They are also
called Relational operators. (Assume x=15 and y =4)
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
x=5
Identity operators are used to compare the objects, not if they are equal, but if they are actually the
same object, with the same memory location:
is not Returns True if both variables are not the same object x is not y
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is z)
print(x is y)
print(x == y)
Python Basics
Operators in Python – Membership Operator
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples.
in Returns True if a sequence with the specified value is present in the object x in y
not in Returns True if a sequence with the specified value is not present in the object x not in y
x = ["apple", "banana"]
print("banana" in x)
Python Basics
Operators in Python –Bit Wise operator
Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b = 13; Now in the binary format their values will be 0011
1100 and 0000 1101 respectively. Following table lists out the bitwise operators supported by Python language with an example each in those,
we use the above two variables (a and b) as operands −
a = 10
a = 0011 1100 b=4
b = 0000 1101
----------------- print("a & b =", a & b)
a&b = 0000 1100 print("a | b =", a | b)
# Print bitwise NOT operation
a|b = 0011 1101 print("~a =", ~a)
a^b = 0011 0001 # print bitwise XOR operation
~a = 1100 0011 print("a ^ b =", a ^ b)
Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken
according to the conditions.
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to
determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
Example-(Pass)
b=200
if b > a:
pass
print("Good Bye")
Python Basics
Python Loops
var = 10
while var > 0:
2 continue statement Causes the loop to print('Current variable value :', var)
skip the remainder of its body and var = var -1
immediately retest its condition prior to if var == 5:
reiterating. break
print("Good bye!")
var = 10
while var > 0:
var = var -1
if var == 5:
continue
print('Current variable value :', var)
print("Good bye!")
Python Basics
Loop Control Statement- Pass
It is used when a statement is required syntactically but you do not want any command or code to execute.
Example Output
for letter in 'Python':
if letter == 'h':
pass
print('This is pass block’)
print('Current Letter :', letter)