Python uses object-oriented programming
Objects are things, or represent things or system. Objects have their own methods and member functions
Kinds of objects (data types):
String objects:
Can be stored within variables, in which case the name of the variable is the object, storing a string literal
All values of a string literal are indexed like a list, so if greeting=“Hello Python”, the function print(greeting[6]) will print the letter P, as that is the 7th value of the string literal and indexing values start at 0
Just because there are 2 l’s doesn’t meant they are indexed the same, when converting a string literal to a list
Using s=greeting[0:7] and then print(s) will print “Hello P”, where s represents a string object.
The command to store certain parts of a substring inside a variable can also be written simply as s=greeting[:7] which leaves out the 0. Same thing with the last value, s2=greeting [7:] stores “Python”
len() functions may also be used in place of number values
Negative numbers may also be used to index the values of a string, with the very last character being indexed as -1, counting down to the left for however many characters there are
Although string objects can be treated like lists, they cannot be changed like lists, meaning they are immutable
Just like a list, string objects can be used like lists are in loops. For instance,
greeting=”Hello there!”
for character in range(len(greeting)):
print(greeting[character])
Built in string object functions:
.upper() will convert the whole string to capital letters
.title() which will convert only the first letter to a capital and all others to being lowercase
.split() will split a string apart, seperating them by whatever is in the parenthesis. If left blank it defaults to a space. If then creates a list of each of the split components. If text=”Hello Python!”, then print(text.split()) will display [‘Hello’,’Python!’]
‘x‘.join(stringObject) is similar to .split, it will join different elements of a list generated from a string object together, using x as an insert between the two, and the list in ()
stringObject.replace(‘x’,’y’) will replace any character x with the character y in the string object stringObject
Operators:
Relational operations:
> | Greater than
< | Less than
>= | Greater than or equal to
<= | Less than or equal to
== | equal to, not to be confused with =
!= | not equal to
Logical operators:
and | This checks if two things are both true
or | This checks if either of two things are true
not | This is the negation of something
If statements:
Will only run certain code under the if statement if a certain condition is met
If (condition) then (operation) | Will run code if a certain condition is true. Example:
if favColor=teal:
print(“Hey that’s my favorite color!”)
If (condition) else (operation) | Will run code if a certain condition is false. Example:
if num/2==2:
print(“Your number is 4.”)
else:
print(“Your number is not 4.”)
elif | stands for else if
if (condition1)
#Code that runs if condition1 is true
elif (condition2)
#Code that will only run if condition1 is false but condition2 is true
elif (condition3)
#Code that only runs if condition1 and condition2 and false, but condition 3 is true
else
#Code that runs if condition1, condition2, and condition3 are all false
Compound if statements: Have 2+ conditions that are evaluated, use and + or
Example:
if (condition) and (condition):
#Code that runs in both are true
if (condition) or (condition):
#Code that runs if either conditions are true
if (condition) nand (condition)
#Code that runs if both conditions are false
if (condition) nor (condition)
#Code that runs if either condition is false
While loops
Uses ‘while’ to run code under it, like in an if statement, under the context that the condition is true
Used in loops to run code multiple times, until a condition is false
def whileExample():
i=0
while i<5:
print(“Hello!")
i=i+1
#This code will print ‘Hello!’ 5 times, as until the 6th time in the loop the i<5 condition is true, each time the loop is run i increased by 1.
Digital number systems:
Binary: Binary is base 2, uses 0s and 1s, and is denoted with the “prefix” 0b.
Hexadecimal: Base 16, 0-F, denoted with the “prefix” 0x.
The print() function can be used to convert things into base 10 numbers, using the indicated “prefixes”. Example print(0b1001) outputs 9.
Bin(x) will convert a number x to binary. For example, b=bin(9) will store 1001 in b
int(‘x’,y) converts a number x, which is base y, from a string to decimal.
hex(x) will convert a number x into hexadecimal, similar to bin(x) with binary.
Storyboards:
Also called pseudocode
General “outline” for what the actual coding will be (UGH FUCK OUTLINES)
Objective: Understand what program you are trying to code, what will it accomplish?
Set up: What the different data types and variables you will need are, note if different things will need to be imported
Generate a simple function: Establish which kinds of functions you will need, and pick a simple one. What will that simple function need to do
Other functions: What are the other functions you will need? What is a simple outline for those functions, including their purpose and general process?