importsysprint('Hello, World')print("It's raining!")print(''' Welcome to Python Have a great day!!! ''')a=Noneprint(a)
Arithmetic in Python
- numeric types: integer and float
- add, subtract, multiply => notice numeric types of results
- powers, division
- integer division & modulo teaming up
- warning: watch for rounding errors
x=25# integer y=17.0# float print(x)print(y)print(x+y)print(x-y)print(x*y)print(x/y)print(x//y)# integer division print(x%y)# modulo print(f'The result is {int(x//y)} remainder {int(x%y)}')print(x**2)print(y**2)x=25y=17.6
rounding errors due to floats
we can cast to int, round(num, digits), etc.
print(x*y)print(int(x*y))print(round(x*y,2))
Casting will truncate (floor) our float
print(int(17.2))print(int(17.9))
Simple Input and Formatted Printing
- Prompt for input()
- Formatted printing 4 ways
name=input('What is your name?\n')print('Hi, '+name+'.')print('Hi, %s.'%name)print('Hi, {fname} {lname}.'.format(lname="Doe",fname="John"))print(f'Hi, {name}.')
Duck Typing
- len()
- try ... except
a=Falsetry:print(len(a))except:print(f'{a} has no length')
# More complex try blocks a='False'b=6c=2try:print(len(a))print(b/c)print(a[47])exceptTypeError:print(f'{a} has no length')exceptZeroDivisionErroraserr:print(f'Cannot divide by zero! Error: {err}')except:print(f'Uh oh, unknown error: {sys.exc_info()}')else:print('No errors! Nice!')finally:print('Thanks for using the program!')# Arithmetic with Strings a="a"b="b"an="an"print(b+an)print(b+a*7)print(b+an*2+a)print("$1"+",000"*3)# Length of a string print(len("Spaghetti"))# => 9 # Indexing into strings print("Spaghetti"[0])# => S print("Spaghetti"[4])# => h print("Spaghetti"[-1])# => i print("Spaghetti"[-4])# => e # Indexing with ranges print("Spaghetti"[1:4])# => pag print("Spaghetti"[4:-1])# => hett print("Spaghetti"[4:4])# => (empty string) print("Spaghetti"[:4])# => Spag print("Spaghetti"[:-1])# => Spaghett print("Spaghetti"[1:])# => paghetti print("Spaghetti"[-4:])# => etti # Using invalid indices # print("Spaghetti"[15]) # => IndexError: string index out of range # print("Spaghetti"[-15]) # => IndexError: string index out of range print("Spaghetti"[:15])# => Spaghetti print("Spaghetti"[15:])# => (empty string) print("Spaghetti"[-15:])# => Spaghetti print("Spaghetti"[:-15])# => (empty string) print("Spaghetti"[15:20])# => (empty string) # .index() function # Returns the first index where the character is found print("Spaghetti".index("t"))# => 6 # print("Spaghetti".index("s")) # => ValueError: substring not found # .count() function # Returns the number of times the substring is found print("Spaghetti".count("h"))# => 1 print("Spaghetti".count("t"))# => 2 print("Spaghetti".count("s"))# => 0 # .split() function # Returns a list (array) of substrings, split on the character passed # If no character is passed, a space is used to split on print("Hello World".split())# => ["Hello", "World"] print("i-am-a-dog".split("-"))# => ["i", "am", "a", "dog"] # .join() function # Works in reverse from what you may be used to with JavaScript # Called on a string that should be used to join each substring from the passed list print(" ".join(["Hello","World"]))# => "Hello World" # ["Hello", "World"].join(" ") JavaScript print("-".join(["i","am","a","dog"]))# => "i-am-a-dog" # .upper() and .lower() transformation functions # These functions do not mutate a='Hello'print(a)print(a.upper())print(a)# Some testing methods # islower() # isupper() # startswith("substring") # endswith("substring") # isalpha() - only letters # isalnum() - letters and numbers # isdecimal() - only numbers # isspace() - only whitespace # istitle() - only title-cased letters (does not account for special words like 'a') print('Once Upon A Time'.istitle())# => True print('Once Upon a Time'.istitle())# => False # Assignment Operators in Python # - Increment (no postfix/prefix) # - Powers and Integer division # - Big Numbers # - Stopping a runaway process (control+c) i=1# i++ does not exist in Python, we have to use i += 1 i+=1print(i)# > 2 i+=4print(i)# > 6 i**=2print(i)# > 36 i//=10print(i)# > 3 i*=10**200print(i)# > 3 followed by 200 0s (all written out) print(float(i))# > 3e+200 (floats are written in scientific notation) i=3i**=10**200print(i)# runaway process! control+c triggers a KeyboardInterrupt to stop it # Meaning of Truth in Python # - numeric types equivalent, but not strings # - conditionals (if, elif, else) # - truth equivalence a=1b=1.0c="1"# print(a == b) # print(a == c) # print(b == c) # if (a == c): # print("match") # elif (a == b): # print("a matches b") # else: # print("not a match") a=[]# Falsy Values: # 0, 0.0, 0j (complex number) # '' # False # None # [] # () # {} # set() # range(0) if(a):print(f'{a} is true')else:print(f'{a} is false')# Logical Operators # We use the keywords 'and', 'or', and 'not' in Python instead of &&, ||, and ! print(TrueandFalse)# > False print(TrueorFalse)# > True print(TrueandnotFalse)# > True # Grouping Conditions # Parentheses can group our conditions, just like JavaScript print(Falseand(TrueorTrue))# > False print((FalseandTrue)orTrue)# > True # Short Circuiting # If we can already determine the overall outcome of a compound conditional # Python will not bother evaluating the rest of the statement # False and (anything else) is always False, so the print is not evaluated Falseandprint("not printed")# Cannot determine overall value so we have to evaluate the right side Falseorprint("printed #1")# Cannot determine overall value so we have to evaluate the right side Trueandprint("printed #2")# True or (anything else) is always True, so the print is not evaluated Trueorprint("not printed")# JavaScript use case of short circuiting # const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; # While loops follow a very similar structure to JavaScript i=0whilei<5:print(f'{i+1}. Hello, world.')i+=1# The 'continue' keyword goes to the next loop # The 'break' keyword exits out of the loop completely i=0whileTrue:print(f'{i+1}. Hello, world.')ifi<4:i+=1continueprint("You've printed 5 times. Goodbye.")break# Identity vs. Equality # - is vs. == # - working with literals # - isinstance() a=1b=1.0c="1"print(a==b)print(aisb)print(c=="1")print(cis"1")print(b==1)print(bis1)print(b==1andisinstance(b,int))print(a==1andisinstance(a,int))# d = 100000000000000000000000000000000 d=float(10)e=float(10)print(id(d))print(id(e))print(d==e)print(dise)b=int(b)print(b)print(b==1andisinstance(b,int))print(a)print(float(a))print(str(a))print(str(a)isc)print(str(a)==c)# Make an xor function # Truth table # | left | right | Result | # |-------|-------|--------| # | True | True | False | # | True | False | True | # | False | True | True | # | False | False | False | # def xor(left, right): # return left != right defxor(left,right):returnleft!=rightprint(xor(True,True))# > False print(xor(True,False))# > True print(xor(False,True))# > True print(xor(False,False))# > False defprint_powers_of(base,exp=1):i=1whilei<=exp:print(base**i)i+=1# We are not hoisting the function declaration, we need to invoke after declared print_powers_of(15)print_powers_of(exp=6,base=7)print_powers_of(2,5)print_powers_of(3,5)print_powers_of(10,5)ifTrue:x=10print(x)print(i)defgreeting_maker(salutation):print(salutation)defgreeting(name):returnf"{salutation}{name}"returngreeting# print(salutation) # Error, salutation is not defined at this scope hello=greeting_maker("Hello")hiya=greeting_maker("Hiya")print(hello("Monica"))print(hello("Raja"))print(hiya("Raul"))print(hiya("Tariq"))
Top comments (0)