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

Lab 06

Uploaded by

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

Lab 06

Uploaded by

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

Applications of ICT

Lab #6
Dr Asad Mansoor Khan
LE Kashaf Raheem
QUIZ 4 (SYN-A)
EVEN SEAT ODD SEAT
Write a Python Write a Python
program that counts program that
the number of counts the number
vowels in a user of even digits in a
input string user input number.
Length is not fixed Length is not fixed
2
QUIZ 4 (SYN-B)
EVEN SEAT ODD SEAT
Write a Python Write a Python
program that counts program that
the number of removes all
consonants (words vowels from a
which are not vowel) user input
in a user input string string.
Length is not fixed Length is not fixed 3
Python Syntax
Lists

4
Python Basics: Lists, Tuples and
Dictionaries
• Built-in data structures in Python:
 List
 Tuple
 Dictionary

• Used a lot due to their utility and flexibility

• Built-in methods provide a handy way to perform simple operations


quickly on these data structures
Python Basics: Lists (Cont’d)
• Indexing starts at 0
print(my_list1[0]) #1

• Other lists (sub-lists) can be an element of a list


my_list4 = [[1,2],[3,4],[5,6],[7,8]]
print(my_list4[0])
print(my_list4[3][0])

• Lists are mutable – meaning that the value of any element (or sub-
element) can be changed at a later stage
 my_list4[3]= [10,12]
 my_list4 = [[1,2],[3,4],[5,6],[10,12]]
Python Basics: Lists (Cont’d)
• Other lists (sub-lists) can be an element of a list
my_list4 = [[1,2],[3,4],[5,6],[7,8]]
print(my_list4[0])
print(my_list4[3][0])

• Toaccess all the elements of a list with sub-lists,


nested loops would have to be used.
Python Basics: Lists
• List functions provide a handy way to manipulate a list
my_list.append(4) #Adds 4 to the last of the list
my_list.extend([1,2,3,4]) #Adds the list passed to this method to the first
list
my_list.insert(2,4) #Inserts 4 at index 2 (Elements after index 2 are
pushed back one index)
my_list.remove(4) #Removes the first occurrence of 4 in the list
my_list.index(4) #Returns the index of the first occurrence of 4 in the list
my_list.sort() #Sorts a list in ascending order
my_list.reverse() #Reverses the order of the elements of the list
Example(s)
• Extending a list
var_list = [1, 2, 3]
var_list.append(9)
print(var_list)
• Reversing
var_list = [1, 2, 3]
var_list.reverse()

13
Tasks
Make sure to comment your code.
Use appropriate variable name
Know the location where file is being saved
Create different files for different tasks
15
Task 1:
Write a Python program that:
1. That takes numbers as input
from the user and adds them to a
list.
2. Squares the numbers in the list
and displays the result.
16
Task 2:
Design an Algorithm and write a
program in python that extracts all unique
characters entered in a sentence. E.g.,
User Enters:
Question for Lab 6
Output is:
There are 15 Unique Letters including:
Q, U, E, S, T, I, O, N, , F, R, L, A, B, 6,
17
Task 3:
Design an Algorithm and write a
program in python check if the user
entered word is in alphabetical order or
not.
If all the letters are not in alphabetical
order, then your program should
display till which letter they are in
alphabetical order.
Next week: MIPS and
Assembly!
Advice:
Download MARS simulator

26

You might also like