1. Variables
πΉ A variable is like a labeled box in your computerβs memory. You can store different types of data inside it β like numbers, words, or lists β and use them later in your program.
πΉ In Python, you don't have to mention the type of data you're storing. Python automatically understands it based on the value.
β For example:
name = "Alice"
age = 25
π’ Here, name
is storing the text "Alice"
, which is called a
string.
π’ age
is storing the number 25
, which is an
integer.
πΈ You can use these variables later in your program like this:
print("Name:", name)
print("Age:", age)
This will output:
Name: Alice
Age: 25
2. Operators
Operators are symbols used to perform actions on variables and values β like adding numbers or comparing them.
- Arithmetic Operators (math):
+
-
*
/
%
**
//
- Assignment Operators:
=
+=
-=
etc. - Comparison Operators:
==
!=
>
<
>=
<=
- Logical Operators:
and
or
not
- Bitwise Operators:
&
|
^
~
<<
>>
β Example: Add two numbers and store the result:
sum = 10 + 5
Now sum
will hold the value 15
.
3. Expressions
An expression is a combination of variables, values, and operators that Python evaluates (solves) to produce a result.
β Example: Calculating the total cost:
total = price * quantity + tax
Here, the expression on the right side of =
is solved
first, and the result is stored in total
.
4. Statements
A statement is a complete instruction that Python can understand and run. Examples include:
- Assignment statement (e.g.,
x = 5
) - Conditional statement (e.g.,
if
) - Loop statement (e.g.,
for
,while
) - Function definition (e.g.,
def my_function():
)
β Example: Checking a score:
if score >= 90:
print("Excellent")
This is an if statement that prints "Excellent" if the score is 90 or more.