Identifiers, variables, data and functions
Learning outcomes/key ideas
In case you want to dig deeper into any of these topics:
Later on, after we’ve had a chance to learn some more Python, this resource (suggested by Mentor Nola) is also helpful for checking your understanding. It doesn’t explain things—rather it just gives you practice problems:
my_number = 123
It’s important to choose names thoughtfully:
We’ll see examples of this as we go through our coding practice together.
A word in the language that has a defined meaning.
Keywords may not be used as identifiers
course = "CSE 8A" # ok!
class = "CSE 8B" # not ok; "class" is a keyword
By case, we mean uppercase vs. lowercase, i.e. capital letters vs. small letters.
if = 3 # not OK; if is a keyword in Python
IF = 4 # allowed, but weird; we typically don't use all upper case for python variables
iF = 5 # allowed, but also a bit weird
If = 6 # allowed, but weird.
What is a statement?
A statement is one unit of code.
It is often (though not always) one line of code:
Here are examples of some simple one line statements in Python:
import math # one statement; imports the functions from the math module
print("Hello, World!") # one statement; a function call to print
my_number = 123 # one statement; assigns the value 123 to the variable my_number
print("square root of ", my_number, "is", math.sqrt(my_number)) # one statement; a function call to print
You can separate multiple statements on a single line using semicolons. (This is uncommon among Python programmers, but you will see it sometimes).
You can also sometimes have a single statement that spans multiple lines.
x = 3; y=4; z=5 # Three assignment statements
# The next three lines are one statement; a single function call to print
print("x=",x,
"y=",y,
"z=",z)
There are various definitions; each of these emphasizes different aspects of what it means to be a variable.
xyz = 123 # integer (abbreviated "int") variable xyz holding an initial value (initialization) of 123
abc = 2.3 # "float" variable abc holding an initial value of 2.3
is_mentor = True # boolean variable (named for George Boole, abbreviated "bool") holding an initial value of True
message = “Hello World” # string variable (a string of characters, abbreviation "str") holding an initial value of “Hello World”
In Python, statements are carried out top to bottom, in order
This is true in most common programming languages (there are a few exceptions).
But it’s different from how we may be used to working with variables in algebra class.
For instance, in an algebra class, if I write:
y = x + 1
x = 7
solve for y
You would all understand how to solve this:
y = 7 + 1
)7 + 1
with 8
y = 8
But that’s not how variables work in Python. The order of assignment statements matters:
This works in Python
x = 7
y = x + 1
print("x = ",x)
This doesn’t. (We’ll show this on the repl during class)
y = x + 1
x = 7
print("x = ",x)
This also doesn’t work:
print("x = ",x)
x = 7
while this does:
x = 7
print("x = ",x)
In Python, the value of a variable can be changed.
This is true in most common programming languages (there are a few exceptions), but different from how we may be used to variables in algebra class.
So here, what do you think will print?
x = 7
print("x = ",x)
x = 9
print("x = ",x)
How about this one?
x = 7
print("x = ",x)
x = x + 1
print("x = ",x)
Examples:
We discussed the difference between print and return.
def squared (x):
''' returns the value of x times itself '''
return x * x
def print_squared (x):
''' prints the value of x times itself '''
print(x * x)
We also talked about the special value None
.
Every function call in Python that doesn’t end in a return
statement behaves as if it had this statement at the end of the function:
return None
The value None
is different from zero; it’s the absence of any value at all.
Two videos you can watch for more information about print
vs. return
What is a Function?
Here is a huge brain dump about functions.
I’ll try to explain each of these ideas during lecture with examples, but don’t worry if you don’t get all of this on the first try.
We need to understand the difference between function definitions and function calls.
return
statement)Functions typically have inputs:
Functions attached to an object are sometimes called: methods
Used as a way to organize execution of a related group of statements to act as a unit to perform a task.
def add (first, second):
''' adds parameters and send back result '''
return first + second # adds parameters and send back result
A string is any sequence of characters between “ “ or ‘ ‘ # use double or single quotes
Examples:
my_college = "Warren"
your_college = "Sixth"
his_college = "ERC"
her_college = "Thurgood Marshall"
their_college = "Revelle"
Note that a number, if it is put in quotes, is still a string:
meaning_of_life = "42"