0% found this document useful (0 votes)
14 views6 pages

02 - Python - Collections

python basics on some course collection

Uploaded by

Arda Sarıtaş
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)
14 views6 pages

02 - Python - Collections

python basics on some course collection

Uploaded by

Arda Sarıtaş
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/ 6

Department of Information Systems and Technologies

CTIS264 – Computer Algorithms


SPRING 2023 - 2024
Lab Guide 2 – WEEK 3
Instructor : Erkan UÇAR
Assistant : Leyla SEZER
Objectives:
 Python Collections
 Lists (sort, reverse, remove operations)
 Tuple
 Dictionary
 for loops
 if- else statement
 Strings, slicing strings
 Random number generation
 Timing

Python Collections : There are some collection data types in Python programming language:
 List is a collection which is ordered and changeable. Allows duplicate members. A list is
denoted by square brackets [ ].
 Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Tuples are represented using parentheses ( ).
 Dictionary is a collection which is unordered, changeable and indexed. No duplicate
members are allowed. Dictionary objects are created using curly braces { }.
Syntax:
dict = {key1 : value1, key2 : value2, … keyN : valueN }

Python Lists (Arrays): Table shows the list methods to make some simple list operations.
Use LIST methods to solve following questions.
Q1. Write a Python program that;
 Inputs 5 integer numbers from the user and put them into a list,
 Finds the average of these numbers, display the average,
 Sorts the list content and display,
 Reverse the list,

Output:
Enter a number: 9
Enter a number: 4
Enter a number: 8
Enter a number: 86
Enter a number: 7

Average is 22.8

List of numbers [9, 4, 8, 86, 7]

Sorted format of the list [4, 7, 8, 9, 86]

Reverse format of the list [86, 9, 8, 7, 4]

Python Tuples;
 You can access tuple items by referring to the index number, inside square brackets.

thistuple = ("apple", "banana", "cherry")


print(thistuple[1])

Output:
banana

 You can use negative indexing that means beginning from the end, -1 refers to the last
item, -2 refers to the second last item, etc.

print(thistuple[-1])

Output:
“cherry”.

 You can specify a range of indexes by specifying where to start and where to end the range.
thistuple =
("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])

Output:
(‘cherry’,’orange’,’kiwi’)

 Once a tuple is created, you cannot change its values. Tuples are unchangeable,
or immutable as it also is called. But there is a workaround. You can convert the tuple into a
list, change the list, and convert the list back into a tuple.
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Output:
(‘apple’,’kiwi’,’cherry’)

 The del keyword can delete the tuple completely. You cannot remove items in a tuple.
 You can concatenate two tuple by using ‘+’ sign. Ex; tuple3= tuple1 + tuple2.

Q2.Write Python program that;


 Creates a list of tuples containing the course name and credits of some courses.
o Courses: ('CS 125',3),('HIST 200',4),('PHIL 243',6),('POLS 304',3), ('ENG 101',3)
 Calculates and displays the total credits for all first year courses in the list. You may assume
that all course codes are exactly 3 digits.

Output:
First year courses:
CS 125
ENG 101

Total credits for first year courses: 6

Q3. Python Dictionaries;


 Example of dictionary creation.

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)

Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

 You can access the items of a dictionary by referring to its key name, inside square brackets
or using get() method. Ex; thisdict["model"] or thisdict.get("model")
 You can change the value of a specific item by referring to its key name.

thisdict["year"] = 2018

 You can print all values in the dictionary, one by one.

for x in thisdict:
print(thisdict[x])
 Loop through both keys and values, by using the items() mehod.

for x, y in thisdict.items():
print(x, y)

 To determine if a specified key is present in a dictionary use the in keyword.

if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")

 You can create a dictionary that contain three dictionaries, nested dictionaries.

myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}

Output:
{'child1': {'name': 'Emil', 'year': 2004}, 'child2': {'name': 'Tobias',
'year': 2007}, 'child3': {'name': 'Linus', 'year': 2011}}
Write a Python program that;
 Creates a dictionary where the keys are the brand and the model of cars, and the values
are the price of the cars. The dictionary should contain the cars and prices shown in the
output below.
 Display the dictionary.
 Display the cars whose prices are over 800.000.
 Change prices of cars which are lower than 800.000, to 800.000.
 Display the final dictionary.

Output:
Original Dictionary:
{'Honda CR-V': 520000, 'Volkswagen Passat': 750000, 'Toyota Yaris': 500000,
'Volkswagen Toureg': 2500000, 'Honda Civic': 650000}

Prices over 800.000:


Volkswagen Toureg 2500000

Updated Dictionary:
{'Honda CR-V': 800000, 'Volkswagen Passat': 800000, 'Toyota Yaris': 800000,
'Volkswagen Toureg': 2500000, 'Honda Civic': 800000}

Q4.
Write a Python program that;
 Gets the name of the user, then generates a password for that user, the rule is;
o Get the characters of the name (from 3 to 6) then put a random number between
(1,900) end of the subtracted string.

Hint: For the random number use the following code segment;
import random
rand_num = random.randint(1,900)

Output:
Enter your name and surname to password: GizemOnses
GizemOnses
Slicing the name (3,6): emOn
Random number (1-900): 771
Generated password emOn771
Q5.
Analysis of the algorithms with empirical analysis;
 Select a specific (typical) sample of inputs.
 Use physical unit of time (e.g., milliseconds).
 Analyze the empirical data.

Write a Python program that;


 Finds the sum of numbers between 0 and 100000, within for loop. Write a function that
calculates this sum and displays the execution time for this for loop (empirical analysis for time
efficiency of the function). And call this function.
 In the main body of the program, find the result of the following formula (which is the formula
to find the sum of the n consecutive numbers), by considering the time efficiency with
empirical analysis. Give 100000 to n.
𝒏
𝒏(𝒏 + 𝟏)

𝟐
𝒊=𝟎

Output:
Sum is 5000050000 required 0.0239837 seconds
Sum is 5000050000.0 required 0.0 seconds if we use formulae

You might also like