Advanced Python

For this workshop we're going to dive deeper into python

Looping

Python has two kinds of loops, for loops and while loops

  • The while loop is used to execute boolean statements
  • Typically, in any programming language, we use a while loop when we don't know how many times we'll iterate over something
  • The for loop iterates over a given sequence
  • We use for loops when we know what range we're going to iterate over

Let's first take a look at the for loop:

In [1]:
# we use the range function for "for" loops
for x in range(5): #Here x is the iterater in the range 0-4
    print(x)
0
1
2
3
4
In [2]:
#We can provide a more specific range in the range function
for num in range(3 , 8): #here we iterate from 3 - 7
    print(num) #also notice we used num instead of x; we
3
4
5
6
7

For loops can iterate over different data structures as well, similar to for each loops in Java

  • For loops work with different data types and data structures

Below are some examples

In [3]:
list_of_nums = [1 , 5 , 10 , 20] #initialize list

for i in list_of_nums: #the variable i iterates through every item in the list
    print(i)
1
5
10
20
In [4]:
name = "SDSU AI" #declaring string

for x in name: #iterate through each character in string
    print(x)
S
D
S
U
 
A
I

While loops

Now that we went over the basics of for loops, let's look at while loops

In [5]:
#like if statements, while loops use conditional statements
a = 0
while a != 5: #this will execute until it's not true
    print(a)
    a = a + 1 #we are manually increasing the iterator
0
1
2
3
4

Importing Modules

When importing modules we usually keep that at the top of our script

  • Python modules are the equivalent of java packages

Let's take a look

In [6]:
#To use math functions we must import the math module
import math

x = math.pi #Here we are calling the math module to use its pi function
print(x)
3.141592653589793
In [7]:
#We can also give aliases to packages
# Some packages usually have a typical alias convention
import math as m #You can change this alias to what you want

x = m.pi #We're doing the same as the code above except we call math with an alias 'm'
print(x)
3.141592653589793

Functions

  • A function is a block of code that only runs when it is called
  • Functions take data and can return data as a result
  • We typically use functions when we need a block of code to execute various times
  • A function is defined by the def keyword and then the name of the function
In [8]:
def my_function(): #Here we defined a simple function
    print("Python is easy to learn")

Notice above how our function didn't print anything
In order for it to execute we must call it:

In [9]:
def my_function():
    print("Python is easy to learn")

my_function() #Calling function
Python is easy to learn

Function Arguments

  • Our functions can also take data input, which is called an parameter
  • You can add as many paramters as you want as long as they are separated by commas
  • When you call your function you must provide as many arguments as parameters listed unless you specify a default parameter
  • You don't have to specify the type of the parameter like in other languages
In [10]:
def a_function(student_name): #Here in the paranthesis we define number of parameters
    print(student_name + " attends SDSU") #We use arguement in print statement

a_function("Bob") #Call function and provide arguement
a_function("John")
Bob attends SDSU
John attends SDSU
In [11]:
def another_function(name, redid): #Here we have two parameters
    print(name + "'s redid is: ", redid)
    
another_function("Hector" , 1082954) #arguements fill parameters in order so first arguement gets first paramter
Hector's redid is:  1082954

Return Values

  • Functions also let you return values
  • Using the return statement returns any value you give it
In [12]:
def mult_by_five(x):
    return x * 5 #returning x value times 5

print(mult_by_five(3))
print(mult_by_five(10))
15
50

Lambda Functions

A lambda function is a one line function with only one expression

  • The format of the lambda function is 'lambda argument: expression'
  • The expression is executed and the result is returned
  • The function is only executed if it is called
  • Lambda functions can be used for various outputs
In [13]:
#Here's an example of a single function
x = lambda a : a + 10 #a is the parameter and x holds the result
print(x(5)) #5 is the argument being passed to the function

#the function above is equivalent to:
def x(a):
    a = a + 10
    return a

print(x(5))
15
15
In [14]:
#This lambda function takes two arguments
multiply_numbers = lambda a,b : a * b  #a and b are parameters
print(multiply_numbers(3,10))#3 and 10 are arguments
30
In [15]:
#you can also include if else chains in lambda functions
x = lambda x : True if (x > 10) else False
print(x(10))
False