Session 1: Introduction To Python: Variables, Expressions, Functions, Parameters, Return
Session 1: Introduction To Python: Variables, Expressions, Functions, Parameters, Return
Introduction to Python:
Variables, Expressions, Functions, Parameters, Return
Time to Download Python!
● Go to https://www.python.org/downloads/
● Download Python 3.8.2
● Open IDLE (IDLE is what we use to code in Python)
IDLE and the Python Shell
LIVE DEMO
of
difference between IDLE and Python Shell
Comments
Help clarify code to human reader
Indicated by #
Example:
#This is a comment
Variables
A name that points to a value, with data types including:
type(“20”)
letter = “abc”
letter = 3.0
#letter = True
Type Conversion
Some variables can be changed from one type to another!
int(2.99999999) → 2
str(5) → “5”
float(9000) → 9000.0
1 + 1
int(“38”)
// gives an integer value by truncating (NOT rounding!) any number behind the
decimal
Examples:
5/2 → 2.5
5//2 → 2
5%2 → 1
Operators Continued
Note: Order of Operations (PEMDAS) still applies in code!
4 - 2 + (3 ** 2 / 3) * 4
Only + and * work with strings. Other operators will yield an error
“Hello” * 3 → “HelloHelloHello”
Functions
A function can be thought of as a bundle of code that will run whenever it is called,
therefore reducing the need to repeat code.
To write a function, you need to set it up with the keyword def , a function name, a
set of parentheses, and a colon.
def mysteryFunc():
Ex:
print(“Go to Publix”)
buyMilk()
Type Conversion
Some variables can be changed from one type to another!
int(2.99999999) → 2
str(5) → “5”
float(9000) → 9000.0
int(19.999999999999)
float(“stranger things”)
int(True)
str(float(38))
int(“4242.564”)
Review - Type Conversion
Remember that certain data types can be converted from one to another.
int(19.999999999999) 19
float(“stranger things”) ERROR
int(True) 1
str(float(38)) “38.0”
int(“4242.564”) ERROR
Parameters
Parameters are placeholder variables that can be included in the function definition
and used throughout the function.
def printSomething(x):
print(x)
We do not know what x is, all we know is that we are printing whatever it is. We will
not know what x is until it is called with a certain argument, which helps the function
be dynamic.
Parameters
#Say I want a function that greets def greet(name):
people
print("Hi, my name is {}".format(name))
def greet():
#OR you can choose to do the line below;
print("Hi, my name is Tiffany")
#it's the same thing
#What if Natalie wanted to use the
#print("Hi, my name is " + name)
same function?
greet() #this is calling the function
greet("Natalie")
greet("Poseidon")
Argument
Arguments are the values that you give to parameters when you call a function.
def printSomething(x):
print(x)
printSomething(5)
Here, 5 is the argument given to take the place of the parameter x, so 5 will then be
printed when the function is called.
Parameters
Remember that parameters are like variables you can use within that function only and
arguments are that variable’s value.
EXAMPLE FUNCTION
Print v. Return
Return statements return a value of the function.
Return values are useful for if you want to use the value evaluated by the
function later.
In contrast, print statements are for the human user to see a string
representation of what is going on. The print function prints something but
returns None.
Print v. Return
def function1(): OUTPUT:
return 5
def function2():
print(6)
print(function1())
print(function2())
Print v. Return
def function1(): OUTPUT:
return 5 5
def function2(): 6
print(6) None
print(function1())
print(function2())
Print v. Return
def returnFunction(): OUTPUT:
def printFunction():
a = returnFunction()
b = printFunction()
print(a)
print(b)
Print v. Return
def returnFunction(): OUTPUT:
a = returnFunction()
b = printFunction()
print(a)
print(b)
Print v. Return
def functionThatPrints(): a = functionThatPrints()
print(5) b = functionThatReturns()
return 5 #can I do a + b?