In Python, a variable is a symbolic name that is used to reference a value stored in the computer's memory. It acts as a label for the data, allowing you to access and manipulate the value without needing to know its exact memory location. Variables make it easier to write and read code by using descriptive names to represent data
Python is a dynamically-typed language, which means you do not need to specify the type of the variable when you create it. The type is inferred from the value assigned to it. For example:
count = 10 # count is an integername = "Alice" # name is a stringpi = 3.14 # pi is a floatcount = "ten" #count is now a stringCan contain letters, numbers, and underscores
Can Start with an underscore
Spaces Not Allowed
AVOID KEYWORDS
Variable Names = short, descriptive
Camel Case
In camel case, the first word is lowercase, and each subsequent word starts with an uppercase letter.
Example: myVariableName, userInput, totalScore
Camel case is often used in Java or other languages but is not the standard convention in Python.
Snake Case
In snake case, all letters are lowercase, and words are separated by underscores (_).
Example: my_variable_name, user_input, total_score
Snake case is the preferred convention for variable names in Python
Recommended by the PEP 8 style guide.
Python developers commonly use this style to maintain readability and consistency
You picking variables names is really important for the readability of your code.
Let's Take a look at some examples for you to decide:
The number of pages in a book:
pages or number_of_pages_in_book
The average grade of a student:
average_grade or student_grades_average
The username for logging in:
username or user_login_name
The number of items in a shopping cart:
cart_items or number_of_cart_items
The name of a movie:
movie_title or title_of_movie_being_watched
The distance between two cities:
distance_between_cities or city_distance_in_km
The result of a math calculation:
result or calculation_output_value
A flag to check if the user is logged in:
is_logged_in or logged_in_status_boolean
The total cost of a purchase:
total_cost or sum_of_all_purchase_amounts
The last item in a list:
last_item or final_list_element
Variables are often described as boxes you can store values in. This idea can be helpful the first few times you use a variable, but it isn’t an accurate way to describe how variables are represented internally in Python.
It’s much better to think of variables as labels that you can assign to values.
You can also say that a variable references a certain value.
To perform a basic print, you can do the following (TRY THESE):
count = 10
print("The Current Count is: ", count)
myvar = "Python"
print("I Like ", myvar, " because it is cool")
Research and List 10 RESERVED or KEYWORDS in Python which you CAN NOT NAME VARIABLES
Complete the Google Form
Create 10 Variables which follow:
5 Must be Snake Case
5 Must be Camel Case
All must follow naming conventions
Complete the following Google Form
You must answer ALL QUESTIONS
ALL CODE MUST BE SAVED
You will use 2 mini-programs in order to lock in use of variables and basic printing
This should be saved in a python file: CH02.01A.py
Both programs can be written in this file
This file should be uploaded to your DROPBOX
To perform a basic print, you can do the following (TRY THESE):
count = 10
print("The Current Count is: ", count)
myvar = "Python"
print("I Like ", myvar, " because it is cool")
The minions need new names. You need to:
Create 3 variables for 3 minions
Assign a name value to each minion
Print the minion names using 3 print statements
The minions need you to keep track of their bananas:
Create 3 variables which keep track of Bob, Stuart, and Kevin's bananas.
Assign a name value to each
Print how many bananas each minion has
Bluey and her family need you to introduce them!
Create 4 variables for the Heeler family members.
bluey, bingo, bandit, chilli
Assign each variable their name as a string.
Print out sentences introducing each family member using print().
BONUS:
Make your print statements look cool or formatted nicely
The Heeler family loves ice cream! Keep track of how many scoops each character has.
Create 4 variables for the number of ice cream scoops:
scoops_for_bluey
scoops_for_bingo
scoops_for_bandit
scoops_for_chilli
Assign each variable a number value (e.g., 2, 3, 1, etc.).
Print out how many scoops each character has, but NOT at the end of the sentence!!!
Bluey and Bingo are putting on a play. You’ll write a Python program that introduces the characters and acts out part of the script.
Create variables for the characters’ names (bluey, bingo, bandit, chilli).
Create variables for what each character will say in the play (bluey_line, bingo_line, bandit_line, chilli_line).
Each line should be a string (like "Let’s play keepy-uppy!").
Print a script where each line starts with the character’s name, followed by their line.
Example Output
Create a variable scene_number and assign it the value 1.
Print a header before the script like this...
Then, change scene_number to 2 and print a new header for the next part of the script, followed by new lines for each character.
BONUS:
Can you figure out how to change the values of variables?
Do you need to create a ton of variables?