@vinaymavi

Agenda

  • Expression vs Statement 
  • Loops
  • Datatype operations and utility functions
  • Function
  • Lemda Function
  • Variable Scoping
  • Data type casting 
  • Operators 
  • List Comprehension

Expression Vs Statement

Expression

Expression are code snippets that return some result.  

#
2+2 

#
True 
#
def add(a,b):
    return a+b
#
add()

#
if True:
    return "True"

"True" if 1 else "False"

Expression

if <expression>:
    <anything>

Loops

Loops

For loop

# For loop
   
for i in range(1,10):
    print 1

for i in range(1,10,2):
    print 1 

for i in range(1,10,2):
    if i == 3:
        continue    
    print 1 

# list iteration 
    

# Tuple Iteration 


#Dictionary Iteration 

Loops

While loop

# While loop

while <condition>:
    <body>

Datatypes operations

Number

# parse to float 
# parse to int

String

# addition 
# formating 
# substring 
# endswith
# indexOf

List

# append
# remove 
# list slice
# list Comprehension 

Dictionary

# Dictionary keys
# Remove dictionary key
# Get value from key
# Iterate Dictionary with loop

Function

def <name>(<agg1>,<arg2>):
    <body>
<name>()

Function(keyword)

def <name>(<arg1>,<arg2>):
    <body>

<name>(<arg2>=<value>,<arg1>=<value>)

Function(default)

def <name>(<arg1>,<arg2>=<value>):
    <body>

<name>(<arg1>=<value>)

Function(variable_length)

def <name>(<arg1>,*<arg2>):
    <body>

<name>(<arg1>=<value>)
def <name>(<arg1>,**<arg2>):
    <body>

<name>(<arg1>=<value>)

Lamda Function


lambda arguments : expression
my_fun = lambda x,y:"True" if x and y else "False"

print my_fun(1,0)

Scoping

str = "TOP MSG"
def print_my_msg():
    def print_local_msg():
        print str
    str = "PRINT MY MSG TOP"    
    print str
    print_local_msg()

print_my_msg()        
print str

Type Casting

Casting


(<casting_type>)<value>

Operators

Operators

Operators

Operators

Operators

Operators

Operators

List Comprehension


# print all animals that not in my_animal list

animal = ["Tiger","Lion","Bear","Cat","Dog","Rat"]
my_animal  = ["Car","Dog"]

List Comprehension


[<item> for <item> in list if <expression>]

Exercise

https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt

Thank you.

Python2

By Vinay Mavi

Python2

Python: Loop,Datatypes operation, function, variable scooping, packages, package import, classes

  • 202