Python is a high-level, interpreted, and easy-to-learn programming language. It is widely used for web development, automation, data science, artificial intelligence, desktop applications, and scripting.
A variable is a named memory location used to store data. Variables help us store and reuse values in a program.
variable_name = value
name = "Shree"
print(name)
Shree
age = 35
print(age)
35
name = "Shree"
city = "Mumbai"
age = 35
print(name)
print(city)
print(age)
Shree
Mumbai
35
name = "Shree"
student_name = "Vinayak"
age1 = 25
1name = "Shree"
student-name = "Vinayak"
class = "Python"
Data Types define the type of value stored in a variable. Python automatically detects the data type based on the assigned value.
| Data Type | Description | Example |
|---|---|---|
| int | Whole Numbers | 10 |
| float | Decimal Numbers | 10.5 |
| str | Text/String Values | "Python" |
| bool | True or False | True |
age = 25
print(age)
25
salary = 25000.50
print(salary)
25000.5
course = "Python Django"
print(course)
Python Django
is_active = True
print(is_active)
True
name = "Shree"
age = 25
salary = 25000.50
print(type(name))
print(type(age))
print(type(salary))
<class 'str'>
<class 'int'>
<class 'float'>
print("Welcome to Python")
Welcome to Python
name = input("Enter Your Name : ")
print(name)
Enter Your Name : Shree
Shree
city = input("Enter City : ")
print("City :", city)
Enter City : Mumbai
City : Mumbai
Operators are special symbols used to perform operations on variables and values. Most operators in Python are similar to those used in C, C++, Java, C# and JavaScript.
| Operator | Description | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| - | Subtraction | 10 - 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| % | Modulus | 10 % 3 |
num1 = 10
num2 = 5
print(num1 + num2)
print(num1 - num2)
print(num1 * num2)
print(num1 / num2)
15
5
50
2.0
Comparison operators are used to compare two values and return either True or False.
age = 18
print(age >= 18)
print(age < 18)
True
False
Logical operators are used to combine multiple conditions.
age = 25
citizen = True
print(age >= 18 and citizen)
True
Conditional statements are used to execute different blocks of code based on a condition. They help programs make decisions.
The if statement executes a block of code only when the condition is True.
age = 20
if age >= 18:
print("Eligible for Voting")
Eligible for Voting
The else block executes when the condition is False.
age = 15
if age >= 18:
print("Eligible for Voting")
else:
print("Not Eligible for Voting")
Not Eligible for Voting
The elif statement allows us to check multiple conditions.
marks = 75
if marks >= 80:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 35:
print("Grade C")
else:
print("Fail")
Grade B
An if statement can be placed inside another if statement.
age = 25
citizen = True
if age >= 18:
if citizen:
print("Eligible to Vote")
Eligible to Vote