0% found this document useful (0 votes)
63 views27 pages

Session 1: Introduction To Python: Variables, Expressions, Functions, Parameters, Return

This document provides an introduction to Python concepts including: 1) How to download and open Python and IDLE. IDLE is used for coding in Python while the Python Shell allows for live testing. 2) Comments, variables, data types, expressions and operators like + - * / % are introduced. 3) Functions are defined as reusable blocks of code that reduce repetition. Parameters allow functions to be more dynamic and take arguments. 4) The difference between print() which displays output and return which provides a value is explained. Return values allow using a function's output.

Uploaded by

Tiffany Xu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views27 pages

Session 1: Introduction To Python: Variables, Expressions, Functions, Parameters, Return

This document provides an introduction to Python concepts including: 1) How to download and open Python and IDLE. IDLE is used for coding in Python while the Python Shell allows for live testing. 2) Comments, variables, data types, expressions and operators like + - * / % are introduced. 3) Functions are defined as reusable blocks of code that reduce repetition. Parameters allow functions to be more dynamic and take arguments. 4) The difference between print() which displays output and return which provides a value is explained. Return values allow using a function's output.

Uploaded by

Tiffany Xu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Session 1

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

Skipped completely by the Python Interpreter

Indicated by #

Example:

#This is a comment
Variables
A name that points to a value, with data types including:

- Int (aka integer, a whole number value)


- Float (a decimal number)
- Str (aka string, anything contained within “ ” or ‘ ’)
- Bool (aka booleans, True/False values)

type(variable_name) gives the value type of the variable


Variables Continued
Must be assigned using = operator

- ALWAYS begin with a lowercase letter or _


- Case sensitive (apple, APPLE, Apple, and ApPLe are all different)
- camelCase and using_underscores
- Can contain digits
- Cannot be a keyword

Variables can be reassigned


Variables Challenge Problems
Is 101dalmatians a valid variable name?

type(“20”)

What is the value of letter after ALL the following assignments?

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

int(“333.3”) → Error #Must be written as int(float(“333.3”))


Expressions
Combination of values, operators, etc. that can be evaluated to a value

1 + 1

int(“38”)

Operators are as follows:

- + and - are addition and subtraction


- * is multiplication
- ** is exponents (i.e. 3^2 is written as 3**2)
- / and // are regular division and floor division
- % (modulus) gives remainders
Operators Continued
/ gives a float (decimal) value

// gives an integer value by truncating (NOT rounding!) any number behind the
decimal

% gives the remaining amount after division

Examples:

5/2 → 2.5

5//2 → 2

5%2 → 1
Operators Continued
Note: Order of Operations (PEMDAS) still applies in code!

What will the following expression evaluate to?

4 - 2 + (3 ** 2 / 3) * 4

Only + and * work with strings. Other operators will yield an error

“Hello” + “Goodbye” → “HelloGoodbye”

“Hello” + str(3) → “Hello3” (Note that “Hello” + 3 yields 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():

#write code here

Note that the code within a function is identified because it is indented.


Functions
What if I wanted to write out instructions for my robot to buy me milk at Publix?

Ex:

print(“Go to Publix”)

print(“Go to the left most aisle and find 2% milk”)

print(“Give cashier $2”)

print(“Walk back home”)


Functions
What if I needed milk again the next week? def buyMilk():

I’d have to rewrite ALL that code! print(“Go to Publix”)

print(“Go to the left most aisle and find 2% milk”)


Functions make life easier and let us reuse useful
code. print(“Give cashier $2”)

print(“Walk back home”)

Now I can tell my robot how buy milk by just


writing:

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(“333.3”) → Error #Must be written as int(float(“333.3”))


Review - Type Conversion
Remember that certain data types can be converted from one to another.

What do the following expressions evaluate to?

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.

What do the following expressions evaluate to?

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.

If there is no return statement, the function has a default return value of


None.

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:

return “this function returns!”

def printFunction():

print(“this function prints”)

a = returnFunction()

b = printFunction()

print(a)

print(b)
Print v. Return
def returnFunction(): OUTPUT:

return “this function returns!” this function prints

def printFunction(): this function returns!

print(“this function prints”) None

a = returnFunction()

b = printFunction()

print(a)

print(b)
Print v. Return
def functionThatPrints(): a = functionThatPrints()

print(5) b = functionThatReturns()

#what do you think a is?

def functionThatReturns(): #what do you think b is?

return 5 #can I do a + b?

#TRY IT YOURSELF ON IDLE!

You might also like