16
Basic Python Notes
import sys
print('Hello, World')
print("It's raining!")
print('''
Welcome to Python
Have a great day!!!
''')
a = None
print(a)
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 = 25
y = 17.6
print(x * y)
print(int(x * y))
print(round(x * y, 2))
print(int(17.2))
print(int(17.9))
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}.')
a = False
try:
print(len(a))
except:
print(f'{a} has no length')
# More complex try blocks
a = 'False'
b = 6
c = 2
try:
print(len(a))
print(b/c)
print(a[47])
except TypeError:
print(f'{a} has no length')
except ZeroDivisionError as err:
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 += 1
print(i) # > 2
i += 4
print(i) # > 6
i **= 2
print(i) # > 36
i //= 10
print(i) # > 3
i *= 10**200
print(i) # > 3 followed by 200 0s (all written out)
print(float(i)) # > 3e+200 (floats are written in scientific notation)
i = 3
i **= 10**200
print(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 = 1
b = 1.0
c = "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(True and False) # > False
print(True or False) # > True
print(True and not False) # > True
# Grouping Conditions
# Parentheses can group our conditions, just like JavaScript
print(False and (True or True)) # > False
print((False and True) or True) # > 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
False and print("not printed")
# Cannot determine overall value so we have to evaluate the right side
False or print("printed #1")
# Cannot determine overall value so we have to evaluate the right side
True and print("printed #2")
# True or (anything else) is always True, so the print is not evaluated
True or print("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 = 0
while i < 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 = 0
while True:
print(f'{i+1}. Hello, world.')
if i < 4:
i += 1
continue
print("You've printed 5 times. Goodbye.")
break
# Identity vs. Equality
# - is vs. ==
# - working with literals
# - isinstance()
a = 1
b = 1.0
c = "1"
print(a == b)
print(a is b)
print(c == "1")
print(c is "1")
print(b == 1)
print(b is 1)
print(b == 1 and isinstance(b, int))
print(a == 1 and isinstance(a, int))
# d = 100000000000000000000000000000000
d = float(10)
e = float(10)
print(id(d))
print(id(e))
print(d == e)
print(d is e)
b = int(b)
print(b)
print(b == 1 and isinstance(b, int))
print(a)
print(float(a))
print(str(a))
print(str(a) is c)
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
def xor(left, right): return left != right
print(xor(True, True)) # > False
print(xor(True, False)) # > True
print(xor(False, True)) # > True
print(xor(False, False)) # > False
def print_powers_of(base, exp=1):
i = 1
while i <= 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)
if True:
x = 10
print(x)
print(i)
def greeting_maker(salutation):
print(salutation)
def greeting(name):
return f"{salutation} {name}"
return greeting
# 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"))
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxgd7r73mtx2q835j85g.jpg)
16