Python basic

Introduction to variables

In Python, variables are used to store information that your program can manipulate and use later. Think of a variable as a box that holds data, like numbers or words. Every variable has a name, and you can think of that name as a label on the box.

1. What is a Variable?

A variable is a name that refers to a value. This value can change as your program runs. It’s a way for you to store data and reuse it whenever you need.

Example:
age = 25
name = "Alice"
 
In this example:
   - The variable age stores the number 25.
   - The variable name stores the text Alice, which is a string (a sequence of characters).


2. Assigning values to variables:

You create a variable by assigning a value to it using the equals sign (=). The variable name comes before the equals sign, and the value you want to store comes after it.

Example:
x = 10  # x now holds the value 10
y = "Hello"  # y now holds the value "Hello"

 
3. Using variables:

After you’ve created a variable, you can use it anywhere in your program. You can print its value, use it in calculations, or even change it.

price = 100
tax = 0.2

total = price + (price * tax)
print(total)  # Output: 120.0


4. Variable names:

  • Variable names should be meaningful, so you and others can understand your code more easily.
  • They must start with a letter or an underscore ( _ ), but they can contain numbers and underscores.
  • Variable names are case-sensitive. For example, myVar and myvar are two different variables.

Examples of valid variable names:   
my_age = 25
user_name = "Alice"

Invalid variable names would be something like:
3cars = 10  # Starts with a number, not allowed!
user-name = "Bob"  # Contains a dash, not allowed!


5. Types of data:

Variables can store different types of data:
   - Integers (whole numbers): age = 25
   - Floats (decimal numbers): height = 1.75
   - Strings (text): name = "Alice"
   - Booleans (True or False values): is_student = True


6. Dynamic typing:

Python is a dynamically typed language, which means you don’t have to declare the type of a variable. The type is inferred from the value assigned. You can also change the type by reassigning a different value.

x = 5          # x is an integer
x = "Hello"    # Now x is a string
  

Variables make your code more flexible and readable. Instead of hardcoding values (like writing 25 everywhere), you can store a value in a variable and update it in one place. This also allows you to easily modify or use the value in multiple parts of your program.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

What is a variable in Python?

A type of data structure
A command used in functions
A fixed value that cannot change
A name that refers to a value
Check Answer

How do you assign a value to a variable?

Using the equals sign (=)
Using a colon (:)
Using the plus sign (+)
Using the arrow (->)
Check Answer

What will the following code print: price = 100, tax = 0.2, total = price + (price * tax), print(total)?

120.0
200
100
120
Check Answer

Which of the following is a valid variable name?

3cars
my_age
user-name
my age
Check Answer

Which of the following is not a data type in Python?

String
Character
Integer
Float
Check Answer