0% found this document useful (0 votes)
37 views9 pages

Python 2

The document is a lab report for a Python programming lab conducted by a student. It includes the student's code for various Python programming exercises on conditional statements, loops, functions, strings and lists. It also includes the output generated by running the code. The code covers concepts like if-else conditions, for loops, functions, input/output, strings and lists.

Uploaded by

Hope H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views9 pages

Python 2

The document is a lab report for a Python programming lab conducted by a student. It includes the student's code for various Python programming exercises on conditional statements, loops, functions, strings and lists. It also includes the output generated by running the code. The code covers concepts like if-else conditions, for loops, functions, input/output, strings and lists.

Uploaded by

Hope H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Report for Python Programming Lab

Programming Lab - EC2602-1


Student Name: SANA K
USN: NNM22EC145
Date: 16-08-2023

Python Code

45
# Paste your Python code here
age=18
if age>=18:
print("you are an adult.")
temperature=25
if temperature >30:

x=10
y=5
if x>y:
C1
print("its a hot day.")
else:
print("its not too hot today.")
2E
if x-y>5:
print("Difference is greater than 5.")
else:
print("Difference is not that big.")
else:
print("X is not that big.")
M2

num = 0
if num > 0:
print("positive")
elif num < 0:
print("negative")
NN

else:
print("zero")
numbers=[1,2,3,4,5]
for sum in numbers:
print("sum")
for i in range(6):
print("hello world",i)
for i in range(6,0,-2):
print("countdown",i)
text="Python"
for char in text:
print("Character",char)

Dept. of Electronics and Communication Engineering, NMAMIT, Nitte 1


Lab Report for Python Programming Lab

code = input("enter the secret code")


if code=="nnm22ec145":
print("the secret code is correct.Alex can go ahead")
else:
print("the secret code is incorrect.Alex cant go ahead")

magic_stones=[1,4,3,4,9,2,4]
collected_stones=[]
for stone in magic_stones:

45
if stone %2==0:
collected_stones.append(stone)
print("collected stones is magic stones",collected_stones)
riddles=[
"i speak without a mouth and hear without ear.I have no body but i come alive with wind.
"the more u take the more u leave behind.What am i?",

C1
"i am taken from mine and shut in the wooden case from which i am never released and yet i a
]
answers=["echo","footsteps","pencil"]
counter=0
total_riddles=len(riddles)
2E
for i in range(len(riddles)):
print("riddles",i+1)
print(riddles[i])
guess=input("enter your answers")
if guess.lower()==answers[i]:
print("correct.proceed to next one")
M2

counter += 1
else:
print("incorrect answers.try again")
if counter==total_riddles:

print("Congratulations.You have found the hidden treasure")


NN

else:
print("U have failed.Better luck next time")

def greet(name):
print("hello,", name, "!welcome to the labyrinth.")

def greet(name):
print("hello,", name, "!welcome to the labyrinth.")
user_name = input("enter your name: ")
greet(user_name)

Dept. of Electronics and Communication Engineering, NMAMIT, Nitte 2


Lab Report for Python Programming Lab

def add_numbers(a, b):


return a + b

num1 = float(input("enter the first number: "))


num2 = float(input("enter second number: "))
result = add_numbers(num1, num2)
print("sum:", result)

45
def find_max(a, b, c):
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c
C1
num1 = float(input("enter the first number: "))
2E
num2 = float(input("enter the second number: "))
num3 = float(input("enter the third number: "))
maximum = find_max(num1, num2, num3)
print("maximum: ", maximum)
M2

def calculate_square(number):
return number ** 2

square = float(input("enter the number: "))


result = calculate_square(square)
NN

print("square: ", result)

def greet(name):
print("hello, ", name, "!welcome!")

user_name = input("enter your name:")


greet(user_name)
print(greet.__doc__)

Dept. of Electronics and Communication Engineering, NMAMIT, Nitte 3


Lab Report for Python Programming Lab

def add_numbers(a, b):


return a + b

num1 = float(input("enter the first number: "))


num2 = float(input("enter the second number: "))
sum = add_numbers(num1, num2)
print("sum: ", sum)

45
# practice questions
# 1
def reverse_string(input_string):
reversed_string = ""
for char in input_string:
reversed_string = char + reversed_string

# test the function C1


return reversed_string

string = "hello, world!"


reversed_str = reverse_string(string)
2E
print(f"the reversed string is: {reversed_str}")

def reverse_string(input_string):
reversed_string = ""
for char in input_string:
M2

reversed_string = char + reversed_string


return reversed_string

# 2
def find_largest_element(numbers):
NN

if not numbers:
return none
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
return largest

numbers_list = [10, 5, 7, 22, 17]


print("input: ", numbers_list)
print("largest element : ", find_largest_element(numbers_list))

Dept. of Electronics and Communication Engineering, NMAMIT, Nitte 4


Lab Report for Python Programming Lab

# 3
def is_palindrome(s):
return s == s[::-1]

input_string = "level"

5
print("input: ", input_string)
print("is palindrome?", is_palindrome(input_string))

14
# 4
def count_even_odd(numbers):
even_count = 0
odd_count = 0
for num in numbers:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
EC
return even_count, odd_count
22
numbers_list = [10, 5, 7, 22, 17]
even, odd = count_even_odd(numbers_list)
print("input:", numbers_list)
print("number of even numbers:", even)
print("number of odd numbers:", odd)
M

# 5
def factorial(n):
if n == 0:
NN

return 1
else:
return n * factorial(n - 1)

num = 5
print("input:", num)
print("factorial:", factorial(num))

Dept. of Electronics and Communication Engineering, NMAMIT, Nitte 5


Lab Report for Python Programming Lab

Output
you are an adult.
its not too hot today.
Difference is not that big.
zero sum sum sum sum sum
hello world 0 hello world 1 hello world 2 hello world 3 hello world 4 hello world
5

5
countdown 6 countdown 4 countdown 2
Character P Character y Character t Character h Character o Character n
enter the secret codennm22ec145

14
the secret code is correct.Alex can go ahead
collected stones is magic stones [4]
collected stones is magic stones [4, 4]
collected stones is magic stones [4, 4, 2]
collected stones is magic stones [4, 4, 2, 4]
riddles 1

with wind.What am i?
enter your answers echo
correct.proceed to next one
riddles 2
EC
i speak without a mouth and hear without ear.I have no body but i come alive

the more u take the more u leave behind.What am i?


enter your answers footsteps
22
correct.proceed to next one
riddles 3
i am taken from mine and shut in the wooden case from which i am never
released and yet i am used by almost every person use me.What am i? enter
your answers pencil
M

correct.proceed to next one


Congratulations.You have found the hidden treasure
enter your name: Sana
hello, Sana !welcome to the labyrinth.
enter the first number: 4
NN

enter second number: 5


sum: 9.0
enter the first number: 6
enter the second number: 7
enter the third number: 8
maximum: 8.0
enter the number: 9
square: 81.0
enter your name:Sana
hello, Sana !welcome!
None

Dept. of Electronics and Communication Engineering, NMAMIT, Nitte 6


Lab Report for Python Programming Lab

enter the first number: 4


enter the second number: 6
sum: 10.0
the reversed string is: !dlrow ,olleh
input: [10, 5, 7, 22, 17]
largest element : 22
input: level
is palindrome? True

5
input: [10, 5, 7, 22, 17]
number of even numbers: 2
number of odd numbers: 3

14
input: 5
factorial: 120

Screenshot of the Output:

EC
22
M
NN

Dept. of Electronics and Communication Engineering, NMAMIT, Nitte 7


Lab Report for Python Programming Lab

5
14
EC
22
M
NN

Figure 1: screenshot of the program

Dept. of Electronics and Communication Engineering, NMAMIT, Nitte 8


Lab Report for Python Programming Lab

5
14
EC
22
M
NN

Figure 2: Output of the Program

Dept. of Electronics and Communication Engineering, NMAMIT, Nitte 9

You might also like