Python Basic and Advanced-Day 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Python Basics & Advanced

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

o Python - Variable Types o Python - CGI Programming

o Python - Basic Operators o Python - Database Access

o Python - Decision Making o Python - Networking


o Python - Loops o Python - Sending Email
o Python - Numbers o Python - Multithreading
o Python - Strings o Python - XML Processing
o Python - Lists o Python - GUI Programming
o Python - Tuples
o Python - Dictionary
o Python - Date & Time
o Python - Functions
o Python - Modules
o Python - Files I/O
o Python - Exception
o Quick recap
o Operator in Python
o Arithmetic Operators

Day-3 o Comparison (Relational) Operators


o Assignment Operators
Agenda o Logical Operators
o Bitwise Operators
o Membership Operators
o Identity Operators
o Decision making in Python
o Loops in Python
Python Basics & Advanced
Quick Recap

Introduction to Python & Its characteristics Python Variables

01 Covered the basic details of Python and why it is so


popular 02 Rules for defining the Python Variables and how to assign
values to these Variables.

Python Datatype Python Operator

03 During this we gone through Numbers, String and Python


collections (List, Tuple, Set and Dictionary.. 04 We will start with operator.
Python Basics
Operators in Python

Operators are used to perform operations on variables and values.

• Arithmetic Operator +, -, * , /, %, ** and //.

• Comparison operator ==, !=, >, <, <=,>=

• Assignment operator =, +=, -=, *=, /=, %=,

//=**=, &=

• Logical operator - AND, OR , NOT

• Identity Operator:- IS and Is Not

• Membership Operator:-IN, NOT IN

• Bit wise Operator:- &, |, ~, <<, >>


Python Basics
Operators in Python- Airthmatic Operator
Arithmetic operators are used with numeric values to perform common mathematical operations:
Assume variable a holds 10 and variable b holds 20, then −

Operator Description Example


+ Addition Adds values on either side of the operator. a + b = 30
- Subtraction Subtracts right hand operand from left hand operand. a – b = -10
* Multiplication Multiplies values on either side of the operator a * b = 200
/ Division Divides left hand operand by right hand operand b/a=2
% Modulus Divides left hand operand by right hand operand and returns remainder b%a=0
** Exponent Performs exponential (power) calculation on operators a**b =10 to the power 20
Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -
//
one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity) − 11.0//3

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)

Operator Name Example


== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Python Basics
Operators in Python- Assignment Operator
Assignment operators are used to assign values to variables:

Operator Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Python Basics
Operators in Python –Logical Operator

Logical operators are used to combine conditional statements:

Operator Description Example


and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4

not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

x=5

print(x > 3 and x < 10)


print(x > 3 or x < 10)
print(not(x > 3 and x < 10))
Python Basics
Operators in Python- Identity Operators

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:

Operator Description Example

is Returns True if both variables are the same object x is y

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.

Operator Description Example

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)

Operator Description Example


& Binary AND Operator copies a bit to the result if it exists in both operands (a & b) (means 0000 1100)
| Binary OR It copies a bit if it exists in either operand. (a | b) = 61 (means 0011 1101)
^ Binary XOR It copies the bit if it is set in one operand but not both. (a ^ b) = 49 (means 0011 0001)
(~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary
~ Binary Ones Complement It is unary and has the effect of 'flipping' bits.
number.
The left operands value is moved left by the number of bits specified by the
<< Binary Left Shift a << 2 = 240 (means 1111 0000)
right operand.
The left operands value is moved right by the number of bits specified by
>> Binary Right Shift a >> 2 = 15 (means 0000 1111)
the right operand.
Python Basics
Decision Making in Python

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.

1 if statements An if statement consists of a boolean


expression followed by one or more statements.

2 if...else statementsAn if statement can be followed by an


optional else statement, which executes when the boolean
expression is FALSE.

3 nested if statementsYou can use one if or else if statement


inside another if or else if statement(s).
Python Basics
Decision Making in Python

Example-(If) Example-(elif) Example-(Nested If)


a = 33 a = 33 x = 41
b = 200 b = 33 if x> 10:
if b > a:
print("b is the greater no, and
if b > a: print("Above ten,")
it is ",b) print("b is greater than a") if x > 20:
elif a==b: print("and also above 20!")
print("a and b are equal") else:
print("but not above 20.")

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

Python has two primitive loop commands:


• With the while loop we can execute a set of statements as long as a condition is true.
• A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary,
a set, or a string).
This is less like the for keyword in other programming languages, and works more like an iterator
method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Example –While loop Example –For Loop


i=1 fruits=['apple', 'banana', 'cheery']
while i < 6: for x in fruits:
print(i) print(x)
i=i+1
Python Basics
Python Loops- while
A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.

Example with else statement


Example i=1
i=1 while i < 6:
print(i)
while i < 6:
i=i+1
print(i) else:
i=i+1 print("i is no longer less than 6")
Python Basics
Python Loops- for

Flow Diagram Examples

for letter in 'Python':


print('Current Letter :', letter)

fruits = ['banana', 'apple', 'mango’]


for fruit in fruits:
print('Current fruit :', fruit)
print("Good bye!")
Python Basics
Loop Control Statements-break

Sr.N Control Statement & Description


o.
Flow Diagram -Break Example- Output
1 break statement Terminates the loop
statement and transfers execution to the for letter in 'Python':
statement immediately following the loop. if letter == 'h':
break
print('Current Letter :', letter)

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!")

3 pass statement The pass statement in


Python is used when a statement is
required syntactically but you do not want
any command or code to execute.
Python Basics
Loop Control statement- Continue
It returns the control to the beginning of the while loop.. The continue statement rejects all the remaining statements in the current iteration of the loop
and moves the control back to the top of the loop.
The continue statement can be used in both while and for loops.
Output
Example
for letter in 'Python':
if letter == 'h':
continue
print('Current Letter :', letter)

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)

print "Good bye!"


Thank you

You might also like