Thumbnail: jekyll

TCM PEH Python Notes A

on under Certs
4 minute read

Intro to Python

(python3)

################

Very first declaration of a Python Script if you run it with ./python.py

#!/bin/python3
rest of script goes here

not necessary if you run with $python3 python.py # like a normal human being. it's not a shell script anyway

Commenting

something(something) # this is a comment in python

Strings

print(“The String”)
print(“newline after this \n”)
print{""“this string runs
multiple
lines”"")
print(“this string is “+”concatenated”)

Math

print(50 + 50)
print(50 - 50)
print(50 * 50)
print(50 / 50)
print (50 + 50 - 50 * 50 / 50) # will PEMDAS it
print(50 ** 50) # exponent 50 to 50th power
print(50 % 6) # modulo
print(50 // 6) # divide with no remainder

Variables and Methods

quote = “This text” # stores the variable ‘quote’ of “this text”

so yeah you don’t have to declare the variable type first

print(quote) # prints the quote variables content

methods are basically functions available for an object, like in Powershell

print(quote.upper()) # prints quote in uppercase
print(quote.lower()) # lowercase
print(quote.title()) # title case (capitalized)
print(len(quote)) # prints char length of quote
name = “Jacob” # string
age = 45 # int
gpa = 1.7 # float
print(int(age)) # will print 45
print(int(45.1)) # will print 45
print("my name is “ + name + ” and i am " + age) # this won’t work, can’t cat a str with int
print("my name is “ + name + ” and i am " + str(age)) # this will work

method above str() turns int into str

age += 1
print(age) # outputs 46

now age will increment 1 since initialization

therefore age = 46

birthday = 1
age =+ birthday # will increment again (by 1)
print(age) # results now in 47

Functions

mini programs that you can reuse, essentially

print(“look, print is a function)
def who_am_i(): # the function definition syntax
…name = “Heath” # four spaces indent, … = spacespacespacespace
…age = 30 # … = 4 spaces, again
…print(”"my name is “ + name + ” and i am age " + str(age))

calling who_am_i will print out the cat’ed phrase with those variables from within that function

pretty sure defining function MUST come before calling function, unlike c++

def add_one_hundred(num):
…print(num + 100) # again, periods = spaces
add_one_hundred(400) # output ends up being 500, do the math, genius
def add(x,y):
…print(x + y)
add(7,7) # outputs 14
def multiply(x,y)
…return x * y # output DOES return product of x,y but doesn’t know to print it out
print(multiply(7,7)) # now it prints out the product
def square_root(x):
…print(x ** .5) # prints the inverse square, ie. square root
square_root(64) # prints the root
def NL():
…print("\n") # new_line makes a…new line

Boolean Expressions

true or false, 1 or 0

print(“Boolean expressions:\n”)
bool1 = True
bool2 = 3 * 3 == 9
bool3 = False
bool 4 = 3 * 3 != 9
print(bool1,bool2,bool3,bool4) # output: True, True, False, False
print(type(bool1)) # output: class ‘bool’

bools make more sense in loops and conditional statements

Relational Operators and Boolean Operators

greater_than = 7 > 5 # this is True
less_than = 5 < 7 # this is True
greater_than_equal_to = 7 >= 7 # this is True
less_than_equal_to = 7 <= 7 # this is True
test_and = (7 > 5) and (5 < 7) # T & T = T
test_and2 = (7 > 5) and (5 > 7) # T & F = F
test_or = (7 > 5) or (5 < 7) # T || T = T
test_or2 = (7 > 5) or (5 > 7) # T || F = T
test_or3 = (7 < 5) or (5 > 7) # F || F = F
test_not = not True # !T = F

google “truth table python”, achieve enlightenment

Certs, Python
comments powered by Disqus