unit-1
unit-1
"""
This is a
multi-line comment
"""
Keywords: Reserved words in Python that cannot be
used as identifiers. Example: if, else, while, class, def,
etc.
1.3 Basic Input and Output Operations
1. Input Operation – input()
• The input() function is used to take input from the
user.
• It always returns a string by default.
Syntax:
variable = input("Enter something: ")
Example:
name = input("Enter your name: ")
print("Hello", name)
If you need numeric input, you must convert it
using int() or float():
age = int(input("Enter your age: "))
2. Output Operation – print()
• The print() function is used to display output on
the screen.
Syntax:
print("Your message here")
Examples:
print("Welcome to Python!")
name = "Aarti"
print("Hello", name)
3. Formatted Output using f-strings
• Python allows you to format strings using f-strings
(available from Python 3.6+).
Example:
name = "Aarti"
age = 20
print(f"My name is {name} and I am {age} years old.")
1.4 Operators in Python
What are Operators?
Operators are special symbols in Python that perform
operations on values and variables. Python has many
types of operators, each used for a specific purpose.
1. Arithmetic Operators
Used to perform basic math operations.
Operator Name Example Output
+ Addition 10 + 5 15
- Subtraction 10 - 3 7
* Multiplication 4*3 12
/ Division 10 / 2 5.0
// Floor Division 10 // 3 3
% Modulus (remainder) 10 % 3 1
** Exponentiation 2 ** 3 8
3. Assignment Operators
Used to assign values to variables and modify them.
Operator Example Meaning
= x = 10 Assign 10 to x
+= x += 5 x=x+5
-= x -= 2 x=x-2
*= x *= 3 x=x*3
/= x /= 2 x=x/2
//=, %=, **= Similar operations
4. Logical Operators
Used to combine multiple conditions.
Operator Description Example Output
True if both conditions x > 2 and x
and True
are true < 10
True if at least one x < 2 or x >
or False
condition is true 10
not Reverses the result not(x > 5) Depends
5. Bitwise Operators
Used for binary-level operations.
Operator Name Example Output
& AND 5&3 1
` ` OR `5
^ XOR 5^3 6
~ NOT ~5 -6
<< Left Shift 5 << 1 10
>> Right Shift 5 >> 1 2
6. Membership Operators
Used to test if a value is part of a sequence (like string,
list).
Operator Example Output
in 'a' in 'apple' True
not in 'x' not in 'apple' True
7. Identity Operators
Used to compare the memory location of two objects.
Operator Example Output
is a is b True/False
is not a is not b True/False
print(x + y) # Arithmetic
print(x > y) # Relational
x += 2
print(x) # Assignment
print(x > 5 and y < 10) # Logical
print('a' in 'apple') # Membership
print(x is y) # Identity
➤ if-elif-else Statement
Checks multiple conditions.
num = 0
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")
➤ Nested if
An if inside another if.
x = 10
if x > 0:
if x < 20:
print("x is between 1 and 19")
2. Loops in Python
Used to repeat a block of code multiple times.
➤ while Loop
Repeats while a condition is true.
i=1
while i <= 5:
print(i)
i += 1
➤ for Loop
Used to iterate over sequences like list, tuple, string.
for i in range(1, 6):
print(i)
➤ Nested Loops
A loop inside another loop.
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")
➤ continue
Skips the current iteration.
for i in range(5):
if i == 2:
continue
print(i)
Output: 0 1 3 4
➤ pass
Does nothing – used as a placeholder.
for i in range(5):
if i == 3:
pass
print(i)
for i in range(5):
print(i)
else:
print("Loop finished successfully")