#!/usr/bin/env python # coding: utf-8 # # Comments # In[1]: # This is a comment. It will not run # In[2]: 1 # This is a comment in the middle of code. It will not run # # Types # In[3]: # If you put something at the end of a code block, Jupyter will print it 1 # In[4]: # Using the "type" keyword will tell you the type of something type(1) # In[5]: # Integers are not the same as floats! type(1.) # In[6]: # There are multiple ways to declare floats type(1.0) # In[7]: # Here is an example of a boolean type(True) # In[8]: # This is the other boolean type(False) # In[9]: # Here is an example of a string type('Hello world!') # In[10]: # Here is an example of a list type([0, 1, 2]) # In[11]: # Here is an example of a NoneType type(None) # # Variable assignments # In[12]: # Python variables are not math variables. # They can be assigned and hold a value x = 1 # Putting a variable at the end of a code block will print it x # In[13]: # You can find the type of what the variable holds by using type type(x) # In[14]: # Variables can be reassigned x = 2 x # In[15]: # The name of a variable is not related to the content at all two = 2 two # In[16]: # As you can see, the variable "two" holds 3 two = 3 two # In[17]: # Variables are case-sensitive X = 4. # In[18]: X # In[19]: x # In[20]: # Variables are case-senstive for any part of the variable Xyz = 3 Xyz # In[21]: xyz = 4 xyz # # Operations (integral types) # In[22]: # Python things can be combined using arithmetic operators 1 + 1 # In[23]: # The result of arithmetic operations can be assigned to variables x = 10. - 5.3 x # In[24]: # The result of an arithmetic operation WILL NOT CHANGE the variable contents x * 2 # In[25]: # as you can see here x # In[26]: x = x * 2 x # In[27]: # An example of floating point division 4.0 / 2.0 # In[28]: # An example of division of integers 4 / 3 # In[29]: # An example of integer division, that returns ONLY the integral part 4 // 3 # In[30]: # Can you guess the output? 7 // 3 # In[31]: # How about now? 10 // 3 # In[32]: 4.0 // 3.0 # In[33]: # An example of "modulus" or "mod" or "finding the remainder" 4 % 3 # In[34]: # What is the answer? 7 % 3 # In[35]: # How about now? 10 % 4 # In[36]: # How about now? 10 % 2 # In[37]: # And now? 143 % 5 # In[38]: x = 1 x = x + 1 x # In[39]: x = 1 x += 1 # This is equivalent to "x = x + 1" x # In[40]: x = 10 x -= 5 x # In[41]: x = 60 x *= 50 x # In[42]: x = 100 x /= 10 x # In[43]: x = 100 x //= 10 x # In[44]: x = 10 y = 10 x == y # In[45]: x = 10 y = 20 x == y # In[46]: # Order of operations matters! # *, /, % happen first, in the order they appear 1 + 3 * 20 % 7 # # Operations (boolean types) # In[47]: # Booleans can be combined using logical ands # The result of logical and is True if and only if both sides are True True and False # In[48]: # Booleans can be combined using logical ors # The result of logical or is True if either side is True True or False # In[49]: # You can put operations that result in booleans and combine them (1 == 1) and (2 == 2) # In[50]: # Logical negation changes True to False not True # In[51]: # and False to True not False # In[52]: not (1 == 1) # # Operations (string, list) # In[53]: # Adding strings is called "concatenation" 'Hello' + ' ' + 'world!' # In[54]: # Aside: can you have a 1-element list? [0] # In[55]: # Aside: can you have a 0-element list? [] # In[56]: # Adding list is also called "concatenation" [0] + [1] # In[57]: x = [0] y = [1] x + y # # Errors # In[58]: # Errors occur when something you do that isn't allowed # Remember: True is supposed to be capitalized! true # In[59]: # Variables must be assigned before being used abcd # In[60]: # Once you assign a variable you can use it abcd = 1 # In[61]: abcd # In[62]: # Dividing by 0 is undefined and thus an error in Python 1 / 0 # In[ ]: