Technical Roadmap

Python Variable Creation

A variable in Python is a name that stores a value in it. You can create a variable by writing a name followed by an equals sign and the value you want to store in it.

name = "Logicoria"

Here name is the variable and "Logicoria" is the value stored in it.

Dynamic Typing in Python

Python is a dynamically typed language. This means you do not need to declare the type of a variable (like "this is a number" or "this is text") when you create one. Python is smart enough to understand the data type based on the value you provide.

You can also change the type of data stored in a variable simply by assigning a new value of a different type. For example, a variable can store a number first and then be updated to store text later without any errors.

Types of Values You Can Store in a Variable

1. String

A string is a text value. You wrap it inside quotes to store it in a variable.

PYTHON
site_name = "Logicoria"
print(site_name)

Output

Logicoria

Explanation: In the above example we created a variable site_name and stored the text "Logicoria" in it. The print() function then displays that value on the screen.

2. Integer

An integer is a whole number with no decimal point.

PYTHON
total_students = 30
print(total_students)

Output

30

Explanation: In the above example we stored the number 30 inside the variable total_students. We do not use quotes for numbers.

3. Float

A float is a number that has a decimal point in it.

PYTHON
average_score = 87.5
print(average_score)

Output

87.5

Explanation: In the above example we stored the decimal number 87.5 inside the variable average_score. Python automatically understands it is a decimal number.

4. Boolean

A boolean stores only two values. Either True or False.

PYTHON
is_passed = True
print(is_passed)

Output

True

Explanation: In the above example we stored True inside the variable is_passed. Always write True or False with a capital first letter.


Creating Multiple Variables at Once

Python lets you create more than one variable in a single line.

PYTHON
a, b, c = 5, 10, 15
print(a, b, c)

Output

5 10 15

Explanation: In the above example we created three variables in one line. Python matches each variable to its value from left to right.

Assigning Same Value to Multiple Variables

You can assign the same value to multiple variables in one line.

PYTHON
x = y = z = 0
print(x, y, z)

Output

0 0 0

Explanation: In the above example we assigned the value 0 to three variables at once. All three variables now hold the same value.

Updating a Variable

You can change the value of a variable anytime by assigning a new value to it.

PYTHON
lives = 3
lives = 2
print(lives)

Output

2

Explanation: In the above example we first stored 3 in lives and then updated it to 2. Python always uses the latest value assigned to a variable.

Note: Python is case sensitive so Score and score are two different variables. Variable names cannot start with a number and cannot have spaces. Use underscore like my_score instead.