───✱*.。:。✱*.:。✧*.。✰*.:。✧*.。:。*.。✱ ───
Assignment: Assignment 3 - Variables, Data Types, and Coding Standards
Variables
- Containers for storing data
- The value can change when a variable is mutable
- Because Python is dynamically typed, variables must be declared and assigned in the same statement
- You can use
Noneif you don’t want a value yet.
- You can use
- In Python, you need a name and a value for a variable.
- You can optionally add a type, such as
str
- You can optionally add a type, such as
cheese: str = "mozarella"
# ^ optional typeAssignment
- Assignment operator (
=)- The value on the right is assigned to the variable on the left.
- IE:
cheese = "mozarella"
- Variables can be re-assigned to any type.
- If you have a type hint for the variable, your IDE should stop you, but Python itself doesn’t care.
Data Types
- Numeric data and non-numeric data
int→ integersfloat→ decimal numbersstr→ a list of characters; text databool→TrueorFalse
age = 69 # int
gpa = 0.1 # float
name = "skibidi toilet" # str
is_cool = True # boolType Checking
- You can check the type of a variable with the
type()function in Python. - You can use this in a conditional expression, such as:
x = 3.0
print(type(x)) # <class 'float'>
print(type(x) === float) # TrueType Conversion (Casting)
- Changes the data type of a variable to another
- Normally you’d have to use a casting operator, such as
as - In Python, you have to use the casting functions, such as
int(),float(), andstr()- These functions can raise errors when converting invalid values
float→intwill truncate
x = "some string"
x = int(x) # ValueError: invalid ltieral for int()
y = 2.99
y = int(y) # ⌊2.99⌋ = 2Math
Operators
+→ addition-→ subtraction*→ multiplication/→ division//→ floor division%→ modulus**→ exponentiation
Division
- In Python, division between two integers always results in a
float, unless you use integer/floor division//:
x = 15
y = 7
x / y # 2.14... (float)
x // y # 2 (int)Modulo
- Returns the remainder of a division operation:
4 % 2 # 0
4 % 3 # 1Strings
- Represented by the
strclass in Python - Concatenation → combining two or more strings
- You can only concatenate strings with other strings (or you raise a
TypeError) - IE:
"me" + "ow"→"meow"
- You can only concatenate strings with other strings (or you raise a
- Interpolation → inserting different things into a string
- You can use the
.formatmethod on strings, or anf-string - IE:
f"i am {x} years old(wherexis anint)
- You can use the
Methods
- You can get the character from a certain index using the
str[idx]syntax.- IE:
"Hello"[0]→"H","Hello"[-1]→"o"
- IE:
- You can get the length of a string using the
len()function- IE:
len("ABC")→3
- IE:
- You can slice strings using the
str[start:stop:step]syntax.- The
stopoption is exclusive, and you can skip options - IE:
"Hello"[0:4]→"Hell","Hello"[0::4]→"Ho"
- The
You can reverse a string with slicing by using
[::-1]
Coding Standards
- The style guide for Python is known as PEP 8
- Use
#for inline (single-line) comments - Use
"""for block (multi-line) comments - Use
snake_casefor naming variables/functions;PascalCasefor naming classes - Empty lines between functions/classes; two empty lines after
importstatements - Keep lines characters
- Use
Comments should describe the why not the what
───✱*.。:。✱*.:。✧*.。✰*.:。✧*.。:。*.。✱ ───