python programming
fundamentals
comments
# this is a comment
# lines that begin with # are comments
# when a line contains a #, everything after the # is a comment
values
"hello there" # this is a value
'hello there' # this is the same value (" or ' make no difference)
42 # this is a value. No " or ' because it is a number
72.19 # this is also a value that is a number
variables
# variables have names
# names must be complete words (no abbreviations)
# variable names must never be a single letter
age # a variable named age
name # a variable named name
# variables can have names with more than one word
street_address # but spaces are not allowed, so you must use _
variables contain values
# a variable called favorite color may contain the value green
# a variable gets content using assignment
# assignment uses an operator
# the assignment operator is =
favorite_color = "green" # the variable is assigned the value green
age = 34 # age is assigned the value 34
more on variables and values
# a variable can be assigned a value
# but values can not be assigned to a variable
title = "Python for Beginners" # yes!
"Python for Beginners" = title # NO!
# Therefore:
# for = think about left and right sides of the =
# left must be a variable
# right can be a variable or a value
left side of =
# ALWAYS a variable
right side of =
# something that provides a value
# a value can be provided by a value (of course!)
# a value can be provided by a variable
what do values and variables do?
# NOTHING (by themselves)
# examples: python code that does nothing
42
name = "Andy" # assignment (does something)
name # but these do nothing
"Hello"
more on right side of =
# can be a function
# a function executes some python code
# a simple function:
addup(1, 2) # addup calculate the sum of two numbers, 3 here
# addup returns the sum of the two numbers (it has the value 3)
# and addup(4, 5) has the value 9
and even more
# a function is thus a value
# a function can therefore only appear on the right side of =
# and a function on its own is just a value
# and values do nothing by themselves
result = addup(1, 2) # calculate and record the sum of 1 and 2
addup(1, 2) # calculate and throw the answer away
# BUT some functions can do something without providing a value
print("hello") # can do something inside the function
recap
# left side of = is ONLY a variable
# right side of = is a variable, value, or function
# values and variables do NOTHING my themselves…
# … except for a function called for what is does inside
print(99) # "inside" prints 99. "Outside", value is not needed