Skip to content

Latest commit

 

History

History
244 lines (156 loc) · 3.25 KB

File metadata and controls

244 lines (156 loc) · 3.25 KB

Variables and Data Types in Python


1. Introduction

In programming, data must be stored so it can be reused later. In Python, we store data inside variables.

Variables are one of the most important foundations of programming. Without variables, you cannot perform calculations, take input, or build logic.


2. What is a Variable?

A variable is a name given to a value.

It allows us to store information and use it later.

Example:

name = "Aarav"
age = 21

Here:

  • name stores text
  • age stores a number

To display them:

print(name)
print(age)

3. Rules for Naming Variables

You must follow these rules:

  1. A variable name can contain letters, numbers, and underscore (_)
  2. It cannot start with a number
  3. It cannot use Python keywords (like if, for, while)
  4. Python is case-sensitive (age and Age are different)

Valid examples:

student_name = "Riya"
marks1 = 90
total_score = 450

Invalid examples:

1marks = 90        # Cannot start with number
class = "BCA"      # 'class' is a keyword

4. Understanding Dynamic Typing

Python automatically understands the type of data you assign.

Example:

x = 10      # integer
x = "Hello" # now string

You do not need to declare the data type separately. Python decides it automatically.

This is called dynamic typing.


5. Core Data Types for Beginners

We focus only on the most important types.


5.1 Integer (int)

Whole numbers without decimals.

age = 18
total_marks = 450

5.2 Float (float)

Numbers with decimal values.

price = 99.99
temperature = 36.5

5.3 String (str)

Text values written inside quotes.

name = "Kiran"
city = 'Mumbai'

Both single and double quotes are allowed.


5.4 Boolean (bool)

Represents only two values:

  • True
  • False

Example:

is_student = True
is_logged_in = False

Booleans are mainly used in conditions (which you will learn later).


6. Checking Data Type Using type()

You can check the type of a variable using type().

Example:

age = 22
print(type(age))

Output:

<class 'int'>

Another example:

name = "Python"
print(type(name))

7. Reassigning Variables

You can change the value of a variable anytime.

score = 50
score = 75
print(score)

The latest value will overwrite the previous one.


8. Multiple Assignment

Python allows assigning multiple variables in one line.

Example:

a, b, c = 10, 20, 30

Or assigning same value:

x = y = z = 5

9. Common Beginner Mistakes

  1. Forgetting quotes in strings:
name = Rahul   # Error
  1. Confusing number and string:
age = "20"
print(age + 5)  # Error
  1. Using keywords as variable names.

10. Practice Exercises

  1. Create a variable country and store your country name.
  2. Create variables for price and quantity.
  3. Print all variables.
  4. Use type() to check their types.
  5. Create a boolean variable called is_available.

Example practice:

country = "India"
price = 500
quantity = 3
is_available = True

print(country)
print(type(price))
print(type(is_available))