2. Variables and Assignment

Quiz: 0/5

Think of a variable as a box where you can store something important, like a number, a word, or a list of items. When you want to use that "something" later, you can simply call the name of the box, and Python will remember what's inside!

What are variables?

In Python, a variable:

  • Has a name that identifies it.
  • Holds a value that you assign to it.

For example:

name = "Alice"
age = 25
is_student = True

Here:

  • name is a variable storing the word "Alice".
  • age is a variable storing the number 25.
  • is_student is a variable storing a True/False value.

Assigning values to variables

You assign a value to a variable using the = symbol. This doesn't mean "equal" in math terms but rather "assign this value to this variable."

For instance:

x = 10  # This assigns the value 10 to the variable x.
y = x + 5  # y gets the value 15 because x is 10.

You can also reassign a variable to a new value:

x = 50  # Now, x holds 50 instead of 10.

Rules for naming variables

  • A variable name must start with a letter or an underscore (_).
  • It can include letters, numbers, and underscores, but no spaces or special symbols.
  • Names are case-sensitive: myVar and myvar are different.

Examples of valid names:

score, high_score, _temp

Examples of invalid names:

3name, my-var, user name

Why use variables?

Imagine writing a shopping list program. Instead of writing the item names repeatedly, you can store them in variables and use them throughout the program. This makes your code cleaner and more flexible.

Example:

item1 = "Apples"
item2 = "Bananas"
item3 = "Oranges"

print("Shopping list:")
print(item1, item2, item3)

Now you can quickly change the value of a variable if needed!

Hands-On Practise

Exercise: Working with variables

Create a program that uses variables to store information about your favorite movie. The program should include:

  1. Title of the movie (a string, e.g., "Inception").
  2. Year it was released (an integer, e.g., 2010).
  3. Rating out of 10 (a float, e.g., 8.8).
  4. Whether you recommend the movie (a boolean, e.g., True).

Bonus Challenge:

Change one of the variable values (e.g., the title or rating) and print the updated summary.

Output:

Quizzes: 0/5

Question 1:

What is a variable in Python?

  • A fixed value that cannot change.
  • A symbol used to perform mathematical operations.
  • A container used to store data that can be accessed and modified.
  • A keyword in Python for defining a function.

Question 2:

Which of the following is the correct way to assign a value to a variable in Python?

  • x == 10
  • x = 10
  • 10 -> x
  • x := 10

Question 3:

Which of these is NOT a valid variable name in Python?

  • my_var
  • _temp
  • 3name
  • score1

Question 4:

What does the following code do? x = 10 y = x + 5

  • Assigns the value 10 to x and 15 to y.
  • Assigns the value 15 to x and 10 to y.
  • Assigns the value 5 to both x and y.
  • Throws an error because x cannot be assigned to y.

Question 5:

Why is it beneficial to use variables in your code?

  • To avoid writing duplicate code and make programs easier to read and modify.
  • To increase the execution speed of the program.
  • To ensure that the values in the program cannot change.
  • To reduce the size of the program.

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonWelcome to Python Programming!Next lessonData types