#!/usr/bin/env python # coding: utf-8 # # Welcome back # # How was lab? Was anything confusing? # # Review # # We covered: # - Jupyter as a powerful calculator # - Operations: addition, subtraction, multiplication, division (two types of division!) # - Special operations: exponentiation, modulus # - Errors # In[2]: 2 + 3 * 4 // 5 - 2 ** (3 % 10) # In[3]: 1 + # In[4]: 2 * * 5 # In[ ]: # Text cells vs code cells - getting out of the text cell # (-4) ** 0.5 # In[5]: (-4) ** 0.5 # In[6]: 0.00000000000000000001 # In[11]: (-3) % 5 # In[9]: -3 // 5 # In[10]: -3 - (-1) * 5 # In[16]: 342 // 12 + 23 ** 1 + 23 - 762 # In[23]: 32 - 762 # In[22]: 9 + 23 # # Reminder: if you're confused, you can test in Jupyter! # In[ ]: # Things that were confusing from earlier today # # Beyond numbers: strings # # Beyond numbers (like `1`), there are also other kinds of "things" in Python. The first one we'll cover are _strings_. # # Strings are sequences of individual _characters_. Roughly, a character is a single piece of text. Don't worry too much about the definition of characters for now. # # In Python, you can define a string by enclosing a piece of text in `"`. We'll see some examples # In[24]: "Hello AddisCoder!" # In[25]: "hello addiscoder" # In[27]: "678" # In[30]: "True" # # Beyond numbers: booleans # # Aside from numbers and strings, there is another "thing" in Python called a _boolean_. A boolean is a True or False. # # The syntax for defining a boolean is to use either `True` or `False`. # In[28]: True # In[29]: False # In[31]: true # # Putting it together: types # # Each of the "things" we've covered has a _type_. Every "thing" in Python has a type. # # You can determine the type of something by doing the following: # In[32]: type(1) # In[33]: type(1.0) # In[34]: type("hello") # In[35]: type(True) # # Converting between types # # You can convert between types using `int`, `float`, `str`, and `bool` # In[36]: type(1.2) # In[37]: int(1.2) # In[38]: int(1.9) # In[39]: int(-1.9) # In[40]: type("1") # In[41]: int("1") # In[42]: int("1a") # In[43]: int("1.2") # In[44]: float(1) # In[45]: float("1.2") # In[46]: str(1) # In[47]: bool(1) # In[48]: bool(2) # In[49]: bool(0) # In[51]: bool(1.0) # In[52]: bool("true") # In[53]: bool("false") # In[54]: bool("") # In[55]: bool(" ") # In[56]: bool("0") # # Operations that (can) convert between types # # Beyond the arithemtic operations we've seen, there are also other kinds of operations. We'll start with equals and not equals. These operations can _change the types_ of the things they operate on # In[57]: 1 == 1 # In[58]: 1 == 2 # In[59]: True == True # In[60]: True == False # In[61]: "hello" == "hello" # In[62]: "hello" == "Hello" # In[63]: "hello" == "hello " # In[64]: 1 != 1 # In[65]: 1 != 2 # In[66]: True != True # In[67]: True != False # In[68]: 1 = 1 # In[69]: 1 == True # In[70]: 2 == True # In[71]: bool(2) # In[72]: 0 == False # In[73]: "" == False # In[75]: int(True) # # Heirachy of equality checking # - String # - Float # - Int # - Bool # In[108]: "" == False # Strings are not compatible with bools # Automatically evaluates to false # In[109]: 1 == True # In[84]: True == "a" # In[85]: True == "True" # Explore other kinds of equals and not equals in lab! # # # Inequalities # # You can also compare numbers # In[78]: 1 > 1 # In[79]: 1 >= 1 # In[80]: 2 > 1 # In[81]: 2 >= 1 # In[82]: 2 < 1 # In[83]: 2 <= 1 # You can also compare things like strings and booleans. Explore what happens in lab! # # # Arithmetic for non-number types # # Python also allows for arithmetic operations for non-number types. This can be very confusing, so let's walk through it slowly. # In[86]: "Hello" + "AddisCoder!" # In[87]: "Hello" + " AddisCoder!" # In[88]: "Hello" + 1 # In[91]: "hello " * 5 # In[92]: "hello " * 0 # In[90]: "hello" // 3 # In[93]: 1 + True # In[94]: 1 + False # In[95]: 1 * True # In[96]: 1 * False # In[97]: "hello " ** 2 # # Advanced topic: variables # # We will briefly cover an advanced topic called _variables_. Don't worry if this is confusing - you do not need understand them today. We will cover it in more detail tomorrow. # # You've seen variables in math, like in $f(x) = x^2$, $x$ is a variable. # # A PROGRAMMING VARIABLE IS NOT A MATH VARIABLE! # # A PROGRAMMING VARIABLE IS NOT A MATH VARIABLE! # # So what is a variable # # A programming variable "holds" some value. This value can be any of the types we've seen so far (they can also be other things, but we'll see this later). # # The value of a programming variable can change! This is NOT TRUE for math variables! # In[110]: x = 1 # In[99]: x = 1 x # In[100]: x = 1 x = 2 x # In[101]: x = 1 x = x + 1 x # In[102]: x = "hello" x # In[103]: x = True x # In[104]: x = 1 y = 2 x + y # In[105]: x = 1 y = 2 z = x + y z # In[106]: x = 1 y = x y # In[ ]: