0% found this document useful (0 votes)
3 views5 pages

Lab Report 04

python
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)
3 views5 pages

Lab Report 04

python
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/ 5

DEPARTMENT OF MECHANICAL ENGINEERING COLLEGE OF E&ME, NUST,

RAWALPINDI

Subject: Fundamental of programming

Lab Number # 04

SUBMITTED TO:

Instructor Name: Dr Asad Mansoor

Lab Engineer: Fatima Ismail

SUBMITTED BY:

Muhammad Talha

DE- 45 Syn A Mechanical Engineering

REG NO # 481402

SUBMITION DATE: 03 March 2024


Objectives:

• To learn about lists and their uses.


• To learn problem-solving techniques.
• To learn about index and a new function, i.e., range in for loops

Related Topic/Chapter in theory class:

• Problem-solving techniques and lists.

Hardware/Software required:

Hardware:
. Laptop

Software Tool:
. VS Code

Task 1: Question statement


• Write a Python program that:
• That takes numbers as input from the users and adds them to a list.
• Squares the numbers in the list and displays the result.

CODE:

numbers = []

while True:
num_input = input("Enter a number (or 'done' to finish): ")
if num_input.lower() == 'done':
break
try:
num = float(num_input)
numbers.append(num)
except ValueError:
print("Invalid input. Please enter a valid number or 'done' to finish.")
squared_numbers = [num ** 2 for num in numbers]
print("Squared numbers of the given number will be:")
for squared_num in squared_numbers:
print(squared_num)
Output:

Task 2: Question statement

• Write a program in python that extracts all unique characters entered in a


sentence. E.g.,
• User Enters:
• question for lab 4
• Output is:
• There are 15 Unique Letters including:
• Q, U, E, S, T, I, O, N, F, R, L, A, B, 4,

CODE:

string_input = input("Enter the Sentence:").upper()


letters_unique = []

for char in string_input:


if char in letters_unique:
continue
else:
letters_unique.append(char)
continue

print("There are", len(letters_unique), "unique letters")


print(letters_unique)
OUTPUTS:

Task 3: Question statement

• Write a python program that finds the length of the largest word in:
• A list
• A dictionary If the list is my_list = [‘write’, ‘a’, ‘python’ ‘program’] and my_dict = {1:
‘write’, 2: ‘ a’, 3: ‘python’, 4: ‘program’]
• The output should be:
• The word with the longest length is: program.

CODE:

my_list = ['write', 'a', 'python', 'program']


my_dict = {1: 'write', 2: ' a', 3: 'python', 4: 'program'}

max_length_list = 0
longest_word_list = ""
for word in my_list:
if len(word) > max_length_list:
max_length_list = len(word)
longest_word_list = word

max_length_dict = 0
longest_word_dict = ""
for word in my_dict.values():
if len(word) > max_length_dict:
max_length_dict = len(word)
longest_word_dict = word

print("The longest word in the list is:", longest_word_list)


print("The longest word in the dictionary is:", longest_word_dict)
OUTPUTS:

Conclusion:

• This lecture covered fundamental concepts of Python programming, focusing


on key data structures such as lists, tuples, and dictionaries.
• These data structures offer versatility and efficiency, enabling developers to
manipulate and organize data effectively. Additionally, we explored essential
techniques including indexing, slicing, and employing built-in functions to
enhance code readability and efficiency.
• By practicing the provided tasks, learners can solidify their understanding and
proficiency in utilizing Python's core features for various programming
challenges.

You might also like