Python Basics

Course by zooboole,

Last Updated on 2025-02-26 16:14:49

Variables

What is a Variable?

A variable is a name that stores a value in programming. It acts like a container where you can store different types of other values, such as numbers, text, lists, etc. It acts as a reference to a value.

Think of a variable as a labeled box:

age = 25
name = "Alice"

Here:

  • age is a variable that holds the number 25.
  • name is a variable that holds the text "Alice".

Rules for Naming Variables

    • Must start with a letter or an underscore (_)
    • Can contain letters, numbers, and underscores
  • x Cannot start with a number (1age)
  • x Cannot use spaces (my age, use my_age )
  • x Cannot be a Python keyword (class, if, else, etc.)

Dynamic Typing in Python

In general, it's required that we indicate the type of the value that a variable labels. We a variable label a number, we should make it clear that it labels a number. But, in Python, that is not needed. Python does that automatically for use by guessing the type of the value. It's called dynamic typing.

Unlike some languages, Python does not require you to declare the type of a variable. You can assign any value to a variable directly.

x = 10        # Integer
y = "Hello"   # String
z = 3.14      # Float

Python figures out the type of data automatically.

Reassigning Variables

The equals to sign in front of the variable name is called assignment operator. It links the value at the right side to the label, the variable name, at the left side. You can change the value of a variable anytime, that's called reassigning.

age = 25
print(age)  # 25

age = 30
print(age)  # 30

Case Sensitivity

Python is case-sensitive, meaning Name and name are different variables. It's very important to choose your way of naming your variables and stay consistant to it.

name = "Alice"
Name = "Bob"

print(name)  # Alice
print(Name)  # Bob

Multiple Assignments

Python allows assigning multiple variables at once. Sometimes, some variable might start with one common value. In such cases, you can assign them all the same value. Later on, some could take different values.

a, b, c = 5, "Python", 3.14
print(a)  # 5
print(b)  # Python
print(c)  # 3.14

You can also assign the same value to multiple variables.

x = y = z = 100
print(x, y, z)  # 100 100 100

Exercise: Try It Yourself!

  1. Create a variable my_name and assign your name to it.
  2. Create a variable my_age and store your age.
  3. Print both variables using print().
  4. Change the value of my_age and print it again.
  5. Test case sensitivity: Create name and Name with different values. Print both.
## Your turn! Fill in the blanks and run this code.
my_name = ___
my_age = ___

print("My name is:", my_name)
print("I am", my_age, "years old")

## Change the value of my_age
my_age = ___
print("Now I am", my_age, "years old")

## Case Sensitivity Test
name = "Python"
Name = "Coding"
print(name)  # Should print Python
print(Name)  # Should print Coding