Fall 2024 - P2
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
Python Variables
Creating a lesson by using Python.
Big Idea 3.1: Variables and Assignments
Learn how variables are used in programs to simplify more complex problems
Lesson
- Variables:
- What are variables?
- Varaible Naming:
- SnakeCase
- PascalCase
- CamelCase
- Putting it all to together
- Variable Types:
- Integers
- Strings
- Boolean
- Float
- Lists
- Dictionaries In Pyhton
- Operators on string
- (+)
- (*)
Variables
Variables
Variables ae used to store some kind of data in program that can be referenced elsewhere (Often more than once)
# Variables
name = "Akshaj"
age = "15 years old"
Grade = "10th grade"
print (name)
print (age)
print (name, age)
Akshaj
15 years old
Akshaj 15 years old
These variables are used to simplify code. Instead of reapeting the staement 15 years old. Everytime I want to refer to it, I just use the variable “age”
Variable Naming
Variable Naming goes over the different ways people like to name their variables. These are based on personal preferences. But their are 3 common ones used amongst all the coders. This is to prevent any confusion when collaborating
Snake Case
Snake case is when you place an underscore (_) between the the two words in the variable name. This underscore is to replace the space betweent the words
# SnakeCase
Scrum_Master = "Arhaan"
print(Scrum_Master)
num_1 = 34
print(num_1)
Arhaan
34
Pascal Case
In Pascal Case, The coder capitalizes the first letter of every word in the variable name. There are no spaces between the words
# PascalCase
ScrumMaster = "Arhaan"
print(ScrumMaster)
TeamLeader = "Akshaj"
print(TeamLeader)
Arhaan
Akshaj
CamelCase
In CamelCase, The coder capitalizes the first letter of the second word of the variable name. This continue onward for every subsequent word
# CamelCase
scrumMaster = "Arhaan"
print(scrumMaster)
teamLeader = "Akshaj"
print(teamLeader)
Arhaan
Akshaj
Putting it all together
**Even though these are different ways to annotate your variables, they work together to support different purposes in code.
Naming Conventions in Python
snake_case: Used for variable names, function names, and method names.
- Example: my_variable, calculate_sum(), get_user_input().
PascalCase: Used for class names.
- Example: MyClass, UserProfile, DataProcessor.
CAPS_SNAKE_CASE: Used for constants.
- Example: MAX_CONNECTIONS, DEFAULT_TIMEOUT, PI.
# Popcorn Hack
# Even though all these work, which of these is standard for Python?
Scrum_Master = "Arhaan"
GroupMaster = "Akshaj"
groupMember = "Mihir"
unkown_member = "Unknown"
print (Scrum_Master, GroupMaster, groupMember, unkown_member)
Arhaan Akshaj Mihir Unknown
Variable Types
Integers
Integers are when numerical whole numbers, both positive and negative are assigned with a variable name. This variable can be called upon later on
# Integers
a = 45
b = -11
c = 0
print(a)
print(b)
print(c)
45
-11
0
Strings
When phrases or sentences are assigned a variable name, that can be called upon later
# Strings
Joke = "Why did the two Java methods get a divorce?"
Answer = "Because they had constant arguments."
print (Joke)
print (Answer)
Why did the two Java methods get a divorce?
Because they had constant arguments.
Boolean
Booleans are true or false statements. You state if if the statement is true of false. You assign a result if it’s true, also a assigned result if it’s false.
# Boolean
is_student = True
if is_student:
print("You are a student.")
else:
print("You are not a student.")
You are a student.
Float
Floats are numbers that aren’t integers and also have decimals within the assigned value
# Float
e = 2.712828
print(e)
2.712828
Lists
When a bunch of numbers or phrases are grouped together, and represented by one list variable
# Lists
group_members = ["Akshaj", "Arhaan", "Mihir", "Keirthan"]
print(group_members)
print(group_members[3])
['Akshaj', 'Arhaan', 'Mihir', 'Keirthan']
Keirthan
Dictionaries In Python
In Python, dictionaries are flexible data structures that hold key-value pairs. Because every value in a dictionary has a distinct key, efficient value lookups and retrievals are possible. They are kinda similar to lists
#Dictionaries In Python
student_info = {
"name": "John",
"age": 19,
"grade": "11th",
"favorite_sports": ["Golf", "Soccer"]
}
print(student_info["name"])
print(student_info["age"])
print(student_info["favorite_sports"])
John
19
['Golf', 'Soccer']
Operators
Operators such as (+) & (*) work with phrases. While many more operators apply on integers
#Operators
n = "my name is "
name = "John"
print (n+name)
print (name * 10)
# With Integers
n_1 = 2
n_2 = 3
n_3 = 4
n_4 = 5
n_5 = 3.141
# all those symbols basicailly work the way we use them in math except (//)
# When you 8 // 3 = It divides it and then rounds down to the nearest whole number
print (n_1 + n_2)
print (n_2 - n_1)
print (n_3 * n_4)
print (n_3 / n_1)
print (n_4 // n_1)
print (n_5 * n_2)
print (n_4 % n_1)
my name is John
JohnJohnJohnJohnJohnJohnJohnJohnJohnJohn
5
1
20
2.0
2
9.423
1