Speed Run Variables
Video Tutorial
Python offers various data types to store information:
- Integer: Whole numbers without decimals (e.g.,
10
). - String: Text enclosed in quotes (e.g.,
"Hello"
). - Float: Numbers with decimals (e.g.,
3.14
). - Boolean: Logical values
True
orFalse
. - List: Ordered collection in square brackets (e.g.,
[1, 2, 3]
). - Dictionary: Key-value pairs in curly braces (e.g.,
{"key": "value"}
). - Tuple: Immutable ordered collection in parentheses (e.g.,
(1, 2, 3)
).
my_number = 1000 # Integer
my_text = "Hello!" # String
my_float = 7.5 # Float
my_bool = True # Boolean
my_list = [1, 2, 3] # List
my_dict = {"a": 1} # Dictionary
my_tuple = (1, 2, 3) # Tuple
Variables are names assigned to data. Use descriptive names and follow naming rules:
- Cannot start with a number.
- No spaces; use underscores (
_
). - No special characters except underscores.
Examples of valid variable names:
item_cost = 1.0
supply_remaining = 2_500_000_000
cto_name = "John Woods"
quarter_value = 0.25
Invalid variable names:
3variable = 5 # Invalid
my-variable = 6 # Invalid
my variable = 7 # Invalid
Arithmetic Operations
Basic arithmetic with integers and floats:
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
String operations:
greeting = "Hello, " + "World!"
print(greeting) # "Hello, World!"
repeat = "Hi" * 3
print(repeat) # "HiHiHi"
Data mutability:
- Mutable: Lists, Dictionaries.
- Immutable: Tuples, Strings.
Attempting to modify an immutable type results in an error:
my_tuple = (1, 2)
my_tuple[0] = 3 # Error: 'tuple' object does not support item assignment