Fall 2024 - P5
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.1 Python Variables
Learning how to use variables in Python
Naming Conventions
There are many naming conventions for variables in programming.
snake_case
In snake case, words in variables are all lowercase and are separated by a comma. These are some examples:
foo_bar
one_two
peppa_pig
camelCase
In camel case, the first word is lowercase, and the first letter of the remaining words are uppercase. These are some examples:
fooBar
oneTwo
peppaPig
PascalCase
In pascal case, The first letter of each word is uppercase. These are some examples:
FooBar
OneTwo
PeppaPig
In Python, we use snake case for data.
Introduction to Variables in Python
A variables is used to store data in Python.
# Booleans
is_student = True
is_teacher = False
print(is_student) # Output: True
print(is_teacher) # Output: False
True
False
# Strings
# Strings are sequences of characters, used to store text.
greeting = "Hello, World!"
name = 'Alice'
print(greeting) # Output: Hello, World!
print(name) # Output: Alice
Hello, World!
Alice
# Dictionaries
# Dictionaries are collections of key-value pairs.
# Key-value pairs allow us to store and retrieve data using keys, which are identifiers.
student = {
'name': 'Alice',
'age': 25,
'courses': ['Math', 'Science']
}
print(student['name']) # Output: Alice
print(student['courses']) # Output: ['Math', 'Science']
Alice
['Math', 'Science']
# Arrays (Lists)
# Lists are ordered collections of items.
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'cherry']
print(numbers) # Output: [1, 2, 3, 4, 5]
print(fruits) # Output: ['apple', 'banana', 'cherry']
greeting = "Hello, World!"
name = 'Alice'
print(greeting) # Output: Hello, World!
print(name) # Output: Alice
[1, 2, 3, 4, 5]
['apple', 'banana', 'cherry']
Hello, World!
Alice
# Dictionaries
student = {
'name': 'Alice', # string
'age': 25, # int
'courses': ['Math', 'Science'] # list
}
print(student['name']) # Output: Alice
print(student['courses']) # Output: ['Math', 'Science']
Alice
['Math', 'Science']
# Arrays (Lists)
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'cherry']
print(numbers) # Output: [1, 2, 3, 4, 5]
print(fruits) # Output: ['apple', 'banana', 'cherry']
[1, 2, 3, 4, 5]
['apple', 'banana', 'cherry']
1. Which of these commands will write "Hello"?
print("Hello")Print(Hello)
print"Hello"
2. Which of these do you use to create an empty dictionary?
()[]
{}
3. Given arr = ['apple', 'banana', 'cherry'], which of these equates to "cherry"?
arr[0]arr[1]
arr[2]