Chapter 1
1. Define variables of different data types, initialize them, and display their values.
# Define variables of different data types
integer_var = 10
float_var = 3.14
string_var = "Hello, Python!"
boolean_var = True
# Display their values
print("Integer Variable:", integer_var)
print("Float Variable:", float_var)
print("String Variable:", string_var)
print("Boolean Variable:", boolean_var)
2. Demonstrate the use of different operators.
# Arithmetic operators
a = 10
b = 3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponentiation:", a ** b)
# Comparison operators
print("Greater than:", a > b)
print("Less than:", a < b)
print("Equal to:", a == b)
print("Not equal to:", a != b)
# Logical operators
x = True
y = False
print("Logical AND:", x and y)
print("Logical OR:", x or y)
print("Logical NOT:", not x)
# Assignment operators
c = 5
c += 2 # Equivalent to c = c + 2
print("Value of c after addition:", c)
3. Demonstrate strings and methods associated with them.
# String demonstration
message = "Hello, Python!"
print("Original Message:", message)
# String methods
print("Uppercase:", message.upper())
print("Lowercase:", message.lower())
print("Length of the string:", len(message))
print("Index of 'Python':", message.find("Python"))
Brief explanation of constants, variables, expressions, keywords, and statements
available in Python:
• Constants: Fixed values like numbers, strings, etc., whose values do not change.
• Variables: Containers to store data values. Their values can change during program
execution.
• Expressions: Combinations of values, variables, operators, and calls to functions that
are evaluated to produce a value.
• Keywords: Reserved words in Python that have special meanings and cannot be used
as identifiers.
• Statements: Instructions that the Python interpreter can execute, such as assignments,
loops, conditional statements, etc.
Chapter 2
1. Check if a given year is a leap year or not.
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
# Example usage:
year = 2024
if is_leap_year(year):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
2. Calculate the sum of the first 20 odd numbers.
python
Copy code
def sum_first_20_odd_numbers():
sum_odd = 0
count = 0
num = 1
while count < 20:
if num % 2 != 0:
sum_odd += num
count += 1
num += 1
return sum_odd
# Calculate and display the sum of first 20 odd numbers
print("Sum of first 20 odd numbers:", sum_first_20_odd_numbers())
3. Find the area of a rectangle using a user-defined function.
def area_rectangle(length, width):
area = length * width
return area
# Example usage:
length = 5
width = 3
print("Area of rectangle with length", length, "and width", width, "is:",
area_rectangle(length, width))
Difference between break and continue statement:
• break statement: Terminates the current loop (for loop, while loop) and transfers
control to the next statement outside the loop.
• continue statement: Skips the rest of the current iteration of a loop and jumps to the
next iteration of the loop.
These statements are used to control the flow of loops in Python programs.