You are on page 1of 4

Python

PEP8 is the standard style guide for Python.

Naming conventions:

 snake_case > all lowercase with underscores between words


 CAPTIAL_SNAKE_CASE > refers to a constant (PI)
 UpperCamelCase > refers to class
 Double underscore (Dunder) > Used for variables we shouldn’t touch (__do_not_touch__)

Getting user input in the powershell prompt

Input() or input(“what is you’re favorite number?”)

Rounding / cutting off the decimal points

Round(x, y)  x is the number, y is the number of decimal points to round to.

Strings
Guess = 9
Print(f”your guess of {guess} was incorrect”)  F strings!!! Sames a ` ${var}` in JS.

Converting data types


decimal = 3.41
Integer = int(decimal)
String(decimal)

If Statements
if name == “Tim”:
print(f”Hi {name}, you’re smelly”)
elif == “sam”:
print(f”Hi {name}, you’re cute”)
else:
print(“Hi nice stranger!”)

NOTE: Python is space sensitive. The indent of the print or the if block needs to be there or
python views it as the next line of code to read. Not part of the if statement.

Truthiness / falsiness
 Falsy values are Empty objects, empty strings, None, and zero.

Logical operators
and or not
if a and b  no caps, just write and, or between the conditions.
Use not to negate. IE:
if not is_weekend:
print(“go to work”)

is vs ==
“is” is reference the same place in memory
== is reference values of variables.

Import Library
Import random  Allow you to use the random.randint(min, max)

Player = Input(“what is your name Player?”).lower()  Converts all the string to lower case.

For loops

For item in iterable_object:


# Do something with item

Ranges
Range(7) – array with 7 (0-6)
Range(1, 8) – array with 6 items, 1-7
Range(1, 10, 2) – Gives odds from 1 to 10
Range(7, 0, -1) counts down from 7 to 1

While loops
While im_tired:
# Do something

Lists (Arrays)

exampleList = [1, “dog”, false, 4.33]

len(exampleList) -> 4

print[0]  Prints 1
print[1]  dog

“dog” in exampleList = true


“cat” in exampleList = false

exampleList.append(“yellow”)  Add a new item (Yellow) to the end of a list


exampleList.extend(1, “red”, 3.44)  Adds multiple elements to the end of a list.
exampleList.insert(index, value)  (2, “dog”)  adds dog to index 2
exampleList.clear()  Removes everying
exampleList.pop(index)  removes item by index, or by default, removes last
item
exampleList.delete()  Remove by value. IE delete(“dog”)
 Deletes the first element of dog. If multiple dog entries,
then it’ll only delete the first element
exampleList.index(“dog”)  Tells us the index of the first element that is “dog”
exampleList.index(“dog”, 1)  index of value “dog” after index 1.
exampleList.index(“dog”, 3, 5)  index of value “dog” Between index of 3 and 5.
exampleList.count(“dog”)  Shows how many times “dog” is in the list
exampleList.reverse()  reverses order, makes 0 the last, 1 the second last…..
exampleList.sort()  sorts ascending/descending
“ “.join(exampleList)  1 dog false 4.33 - It joins the elements to one string
exampleList[start:end:step]  has to use at least start: --- [2:], otherwise python thinks
your referring to a specific index…. Can slice from -3 to get last
3 elements of the list
 It is exclusive
 You can do  [:-1] which will give you from index 0 to the
second last element.
 You can do  [::2] which will give you index 0 to the end
on intervals of 2.. including index 0, 2, 4, 6, 8

COMPREHENSION with LISTs


numbers = [1, 2, 3, 4]
double_numbers = [num *2 for num in numbers]  This basically says for each num in numbers,
append num*2
 Good to conver all values to in a list to a
type
Conditional logic for comprehension.
evens = [num for num in numbers if num % 2 ==0]  add an if condition to the end.

Condition with else..

[num*2 if num % 2 ==0 else num/2 for num in numbers]  If num is even, multiply by 2, else num/2

Nest Lists
A list with list elements  nums = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Nums[0][2]  3
Nums[3][1]  8
Dictionary
Instructor = {
“name”: “bob”,
“age”: 3,
“gender”: “male”,
}

cat = {“name”: “eddy”, “age”: 100, “gender”: “male”}


cat2 = dict(name=”Kiki”, age=5}

cat[“name”]  Gets you eddy.

Iterating over Dictionaries.


Loop through Values:
For value in cat.values()
Print(value)  Will print out the value (eddy) of each key (name

Cat.values() – gets values


Cat.keys() -- gets keys
Cat.items() -- gets key and value

For k, v of cat.items():
Print(f”Key {k}, value: {v}”)

if name in cat:
print(f”Cat’s name is: {name}”)

cat.clear() -> removes all key value pairs in a dictionary. Completely removes key and value to have {}
newList = cat.copy()  Creates a copy of cat (doesn’t reference the location of memory
{}.fromkeys([“name”, “age”, “gender”], “unknown”)  Gives you name: unknown,
age: unknown, gender unknown
** Usually used to create default values.
Cat.get(“name”)  will return name value if it exists, otherwise it’ll give “none” vs an error
Cat.pop(“name”)  removes key value pair of “name”  gets rid of name: eddie. Leaving the other K/P

k/p Key value pair

cat.popitem(()  removes a random key value pair.


Cat.update()  allows you to set a dictionary default values based on another dictionary
 It’ll overwrite values that are already there and create new values if not already

You might also like