0% found this document useful (0 votes)
13 views

Experiment no 1_python

Pyt

Uploaded by

atharvaaher1910
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Experiment no 1_python

Pyt

Uploaded by

atharvaaher1910
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Students work :

1. Identifiers, Keywords, Indentation, Variables, and Comments


# This is a single-line comment
"""
This is a multi-line comment
"""

# Valid identifier and variable assignment


my_variable = 10

# Indentation example
if my_variable > 5:
print("Variable is greater than 5")

2. Basic Data Types (Numeric, Boolean, Compound)


# Numeric types
integer = 5
float_num = 5.5
complex_num = 3 + 4j

# Boolean type
is_valid = True

# Compound type (List)


my_list = [1, 2, 3, 4, 5]

print("Integer:", integer)
print("Float:", float_num)
print("Complex:", complex_num)
print("Boolean:", is_valid)
print("List:", my_list)
3. Operators
# Arithmetic operators
a = 10
b=3
print("Addition:", a + b)
print("Division:", a / b)

# Logical operator
print("Logical AND:", a > 5 and b < 5)

# Membership operator
my_list = [1, 2, 3]
print("Is 2 in list?", 2 in my_list)

# Identity operator
print("Is a equal to b?", a is b)

4. Control Flow Statements


# If statement
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive")

# If-else statement
if num % 2 == 0:
print("The number is even")
else:
print("The number is odd")

# Nested if statement
if num > 0:
if num < 10:
print("The number is between 1 and 9")
else:
print("The number is greater than or equal to 10")
else:
print("The number is non-positive")

# Simple if-else
num = 15
if num % 2 == 0:
print("Even number")
else:
print("Odd number")

# Nested if
if num > 0:
if num < 20:
print("Number is between 0 and 20")

5. Looping in Python
#While loop
# Print numbers from 1 to 5
i=1
while i <= 5:
print(i)
i += 1

#For loop
# Print elements of a list
for item in [1, 2, 3, 4]:
print(item)

# Nested loop
for i in range(1, 3):
for j in range(1, 3):
print(f"i={i}, j={j}")

6. Loop Manipulation
# Using break, continue, and pass
for i in range(5):
if i == 2:
continue # Skip the rest of the loop when i is 2
if i == 4:
break # Exit the loop when i is 4
print(i)

# Pass statement
for i in range(5):
pass # Does nothing, used as a placeholder
7. Input/Output Functions

# Taking user input and displaying output


name = input("Enter your name: ")
print("Hello,", name)

c) Looping in Python
# While loop
i=1
print("Numbers from 1 to 5 using while loop:")
while i <= 5:
print(i, end=" ")
i += 1

# For loop
print("\n\nNumbers from 1 to 5 using for loop:")
for i in range(1, 6):
print(i, end=" ")

# Nested loop
print("\n\nNested loop example (Multiplication table):")
for i in range(1, 4):
for j in range(1, 4):
print(f"{i} * {j} = {i * j}")

8. Decorators
# Decorator example
def decorator(func):
def wrapper():
print("Before the function call")
func()
print("After the function call")
return wrapper

@decorator
def say_hello():
print("Hello, World!")

say_hello()

9. Iterators and Generators

# Iterator example
my_list = [10, 20, 30, 40]
iterator = iter(my_list)

print("Using an iterator:")
print(next(iterator))
print(next(iterator))
print(next(iterator))
print(next(iterator))
10. Generator

# Generator example
def generate_numbers(n):
for i in range(1, n + 1):
yield i

gen = generate_numbers(5)

print("Using a generator:")
for num in gen:
print(num)

Programs
1. Fibonacci Series in Python using While Loop
2. Python Program to Print all Prime numbers between 0 to
100
3. The program takes in a number and prints the table of a
given number.
4. User must enter 5 different values and store it in separate
variables.
2. Then sum up all the five marks and divide by 5 to find
the average of the marks.
3. If the average is greater than 90, “Grade: A” is printed.
4. If the average is in between 80 and 90, “Grade: B” is
printed.
5. If the average is in between 70 and 80, “Grade: C” is
printed.
6. If the average is in between 60 and 70, “Grade: D” is
printed.
7. If the average is anything below 60, “Grade: F” is
printed.

You might also like