let, const, or var in front of something that I want to use as a variableglobal reserved word
x and have many x’s throughout the program, you may call the wrong one:lambda functions that allow you to create anonymous functions within other functions;x = 5)## must proceed each comment lineThere is a work around that will allow you to use a string literal not assigned a variable to create a multiline comment
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
You can specify the type of data a variable should be via casting
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Python will allow you to assign values to multiple variables in one line
x, y, z = "A", "B", "C"
print(x) #prints x
print(y) #prints y
print(z) #prints z
You can assign the same value to multiple variables
x = y = z = "Orange"
print(x)
print(y)
print(z)
You can unpack a collection of values and extract each value into variables
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
print() is the output variable in Pythonglobal keyword to create a global variable inside a function| Type | Name |
|---|---|
| Text | str |
| Numeric | int, float, complex |
| Sequence | list, tuple, range |
| Mapping | dict |
| Set | set, frozenset |
| Boolean | bool |
| Binary | bytes, bytearray, memoryview |
| None | NoneType |
int = whole numberfloat = decimal numbercomplex = combo of letters and numbersRandom number generation:
import random
print(random.randrange(1, 10)) #prints random numbers between 1 and 10
"""<string>"""Loop through string
for x in "banana":
print(x)
String Length
a = "Hello, World!"
print(len(a))
Check contents of string
# Contains value
txt = "The best things in life are free!"
print("free" in txt)
# Does NOT contain value
txt = "The best things in life are free!"
print("expensive" not in txt)
Sting slicing
b = "Hello, World!"
#From beginning
print(b[:5]) #Hello
#From end
print(b[6:]) #World!
#Between
print(b[2:6]) #ello
.upper() = to uppercase.lower() = to lowercase.strip() = remove whitespace.replace(val1, val2) = replace val1 with val2.split("delimiter") = break the string apart at the delimiter (i.e. ,, ` , :`, etc.)str1 + str2 = str1str2str1 + " " + str2 = str1 str2{} as place holders combined with .format(variable)age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
Escape Characters for strings
| Code | Result |
|---|---|
| ' | Single Quote |
| \ | Backslash |
| \n | New Line |
| \r | Carriage Return |
| \t | Tab |
| \b | Backspace |
| \f | Form Feed |
| \ooo | Octal value |
| \xhh | Hex value |
| Operator | Example | Same As |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| //= | x //= 3 | x = x // 3 |
| **= | x **= 3 | x = x ** 3 |
| &= | x &= 3 | x = x & 3 |
| |= x | |= 3 | x = x | 3 |
| ^= | x ^= 3 | x = x ^ 3 |
| »= | x »= 3 | x = x » 3 |
| «= x | «= 3 | x = x « 3 |