Variables and Basic Python Datatypes

Variables

Variables reference a value


variables
bar = 1
foo = "two"
que = 3.0
por = bar

Variables: Knowledge Check


Why doesn’t this work?


variables
var_1 = var_2
var_2 = 2

Variables: Knowledge Check


What is the value of foo?


variables
bar = 12
foo = bar
bar = 20
print(foo)

Basic Types

  • Number
  • Boolean
  • String
  • List

Numbers

  • Python supports ints, floats, complex
  • Operators return appropriate types


numbers
my_sum = 42 + 2.7182818  # 44.7182818
my_product = 42 * 2   # 84
my_division = 30 / 2  # 15.0
my_floor_division = 30 // 2  # 15
my_expontential = 10**6  # 1_000_000

Numbers: Intuition Check

How do you think complex numbers behave when added by an int?


numbers
my_complex = complex(1, 1)

my_complex + 1

Boolean

  • Booleans can be either True or False


boolean
so_true = True
so_false = False

my_var1 = so_true and so_false
my_var2 = so_true or so_false

Boolean

  • Everything in python is “truthy” except:
    • 0, empty things, None, False, …


boolean
bool(10)
bool(1.0)
bool("")
bool(0)

Strings

Strings represent characters

strings
my_str = "Bloom's Taxonomy"

str_concat = my_str + " is great!"

str_repeat = "a" * 2

lower_case = my_str.lower()
upper_case = my_str.upper()

Strings: Intuition Check

What do these methods do?


strings
my_str = "a farewell to arms"

my_str.title()
my_str.startswith("a")

Lists

  • Lists hold multiple values, which can be different types.
lists
my_list = [1, 2, True, 0.0]

len(my_list)  # get the length of the list
my_list[0]  # gets first value
my_list[-1]  # gets last value
my_list[1:2]  # gets middle values
# append value to end
my_list.append("Grapes of Wrath")
# join two lists together
concated = [1, 2, 3] + [4, 5, 6]  

Lists

  • Lists support iteration.


lists
some_phrase = ["Heghlu'meH",  "QaQ", "jajvam"]

for word in some_phrase:
    print(word) 

Lists: Knowledge Check


lists
my_list = [1, 2, 3]

len(my_list)
my_list[1] 
my_list.append(8)
my_list[-2]

Exercise

http://tinyurl.com/59d2s9yv