coding180 icon
Coding180
5 - Python variables

5 - Python variables

Home > Tutorials > Python for beginners > Getting started with Python


In Python, a variable is a named storage location that holds a value. When you create a variable, you can specify the name and value that you want to store in it.

The value can be of any data type, such as a string, integer, or float. You can then access the value stored in a variable by referencing its name.

Imagine that you have a box with a label on it that says "my_favorite_color". Inside the box, you can put a piece of paper with the name of your favorite color written on it, such as "blue" or "green". This piece of paper is like a value that you can store in a variable.

In Python, you can create a variable like this:

my_favorite_color = "blue"

In this example, my_favorite_color is the name of the variable, and "blue" is the value that is stored in it.

You can then access the value stored in the variable by referencing its name:

print(my_favorite_color) # Output: blue

In this case, the print function is used to display the value of the my_favorite_color variable on the screen. The output will be "blue", which is the value that was stored in the variable.

You can also change the value stored in a variable by assigning a new value to it:

my_favorite_color = "green" print(my_favorite_color) # Output: green

In this example, the value of the my_favorite_color variable is changed from "blue" to "green", and then the new value is displayed using the print function. The output will be "green", which is the updated value of the variable.

Python Variable Quiz

  1. How can you access the value stored in a variable in Python?
    1. A. By using the print() function
    2. B. By using the input() function
    3. C. By using the scan() function
    4. D. By using the store() function
  2. What is the output of the following code in Python?
    my_favorite_color = "blue" print(my_favorite_color)
    1. A. "blue"
    2. B. "green"
    3. C. "my_favorite_color"
    4. D. "print"
  3. What is a good practice when naming variables in Python?
    1. A. Use short and generic names
    2. B. Use long and specific names
    3. C. Use names that are not case-sensitive
    4. D. Use names that are not meaningful
  4. What is the value of the my_favorite_color variable after the following code is executed in Python?
    my_favorite_color = "blue" my_favorite_color = "green"
    1. A. "blue"
    2. B. "green"
    3. C. "my_favorite_color"
    4. D. "print"

user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel