1.Variables in Python
A variable is a name used to store a value in a program. In Python, we don't need to declare the variable type explicitly. Python automatically identifies the type of value.
Example
`name = "Gowtham"
age = 20
price = 99.5
print(name)
print(age)
print(price)`
Output:
Gowtham
20
99.5
In this example:
- name stores a string value.
- age stores an integer value.
- price stores a floating-point value.
- Rules for Naming Variables
- A variable name can contain letters, numbers, and underscores.
- A variable name must not start with a number.
- Variable names are case-sensitive.
- Spaces are not allowed in variable names.
Reserved words cannot be used as variable names.
Valid variable names:
name = "Python"
age = 20
student_name = "Ravi"Invalid variable names:
2name = "Ravi" # Starts with a number
student name = "Ravi" # Contains a space
class = 10 # Reserved word
2.Reserved Words in Python
Reserved words, also called keywords, are words that have special meanings in Python. They are used to define the structure and behavior of a program.
We cannot use these keywords as variable names or function names.
Common Python Keywords
3.Data Types in Python
A data type specifies the kind of value stored in a variable. Python provides several built-in data types.
1.Numeric Types
Integer (int) — Stores whole numbers.
age = 20
print(age)
Float (float) — Stores decimal numbers.
price = 99.5
print(price)
Complex (complex) — Stores complex numbers.
number = 2 + 3j
print(number)
2.String (str)
A string stores text inside single or double quotation marks.
name = "Python"
print(name)
Output:
Python
3.Boolean (bool)
A Boolean represents either True or False.
is_student = True
print(is_student)
Output:
True
4.List (list)
A list stores multiple items in a single variable. Lists are ordered and mutable, meaning their contents can be changed.
fruits = ["apple", "banana", "mango"]
print(fruits)
5.Tuple (tuple)
A tuple stores multiple items. Unlike lists, tuples cannot be changed after creation.
colors = ("red", "green", "blue")
print(colors)
6.Set (set)
A set stores unique values and does not guarantee a fixed order.
numbers = {1, 2, 3, 3}
print(numbers)
The duplicate value 3 is stored only once.
7.Dictionary (dict)
A dictionary stores information as key-value pairs.
`student = {
"name": "Ravi",
"age": 20
}
print(student["name"])`
Output:
Ravi
8.None Type (NoneType)
None represents the absence of a value.
result = None
print(result)
Output:
None

Top comments (0)