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

Programming - Week 3 - Part 2

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)
7 views

Programming - Week 3 - Part 2

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/ 13

Introduction to Lists

my_list = []

my_list.append(5) #add 5 to my_list as first element


my_list.append(7) #append() is a method
my_list.append(9)
my_list.append(11)
● Storage for values using only one
variable
print(my_list[0]) #prints fist element of the list
print(my_list[-1]) #prints last element of the list

● These values are called items or print(len(my_list)) #prints how many items
elements my_list[0] = 6 #change the value of first element
● We can access, add, remove print(f"my first element is: {my_list[0]}")

values from the list using


methods.
>5
● Index starts from 0 >11
>4
● len() function will return how >my first element is: 6

many items in that list

71
Introduction to Lists
my_list = [2,3,5,11,13]
print(my_list)

my_list.insert(3,7) #insert to specific position


print(my_list)

● Add my_list.pop()
○ append -> method print(my_list)
○ insert -> method
my_list.pop(3)
● Remove print(my_list)
○ pop() -> method
○ remove -> method, remove first my_list.remove(3)
print(my_list)
occurrence of the value
● Sort print(f"max: {max(my_list)} min: {min(my_list)}")
○ sorted() -> function
○ sort() -> method >[2, 3, 5, 11, 13]
● Others >[2, 3, 5, 7, 11, 13]
○ max() -> function >[2, 3, 5, 7, 11]
>[2, 3, 5, 11]
○ min() -> function >[2, 5, 11]
○ sum() -> function >max: 11 min: 2

72
Programming Task 31
● Please create a list
● add 3 items
● print all items
● print the first one
● print the length
● delete the second item
● print all items again

>["my first Item, 'my second item', 'my last item']


>my first Item
>List length is 3
>['my first Item, 'my last item']

73
Programming Task 32
● Please create a list
● add 3 items
● print all items
● change the value of first item to "my new item"
● print all items again
>['my first item, 'my second item', 'my last item']
>['my new item', 'my second item', 'my last item']

74
Programming Task 33
● Please create loop which ask user for input and append them to the
list. If user writes exit, print the list and finish the program.

>Please enter an input: Mahmut


>Please enter an input: Agent 47
>Please enter an input: Kratos
>Please enter an input: Geodude
>Please enter an input: Heisenberg
>Please enter an input: exit
>['Mahmut', 'Agent 47', 'Kratos', 'Geodude', 'Heisenberg']

75
For Loop
my_list = [0,1,2,3,4,5]

for i in my_list:
print(i)

● Please don’t use break and >0


>1
continue! >2
>3
● Syntax is easy and works like >4
conditional statements >5

● Loop will stop iterate when reach my_list = [0,1,2,3,4,5]


predetermined step.
Much more suitable for collection
for i in range(0, 5, 2): #range(start, stop, step)
● print(i)
operations
in range arguments, only stop is
>0
● >2
mandatory, start and step are >4

optional

76
Programming Task 34
● Please use the list which created at task 33 and print the list items.

>Mahmut
>Agent 47
>Kratos
>Geodude
>Heisenberg

77
Programming Task 35
● Please create loop which ask user for input as string and print every
character separately (new line)

>Please enter an input: Mahmut


>M
>a
>h
>m
>u
>t

78
Programming Task 36
● Please create loop which calculate the total points of last round.

>total 7 points earned. >raw_points = [1, 2, 1, 3]


>
>#enter loop here
>
>print(f"total {total_points} points earned")

79
Programming Task 37
● Please create loop which calculate the sum of positive numbers.

>total 12 points earned. >raw_points = [1, -2, 1, 3, -5, 7, 0]


>
>#enter loop here
>
>print(f"total {total_points} points earned")

80
Programming Task 38
● Please create a tic tac toe grid but ask user for “table size” first.

>please input table size: 3 >


>|_|_|_| >
>|_|_|_| >
>|_|_|_| >
>

81
Programming Task 39
● Please create a spruce. Ask user for spruce height. User will enter
positive integer.

>Spruce height: 3 >


> >
> * >
> *** >
>***** >
> *

82
End of the Week
Thanks Materials
mahmutcankovan@mu.edu.tr
● Think Python, 2nd Edition, How to Think
Like a Computer Scientist, Allen Downey,
Green Tea Press (2015)
● www.freecodecamp.com
Homework(s) ● www.geeksforgeeks.com
● null

83

You might also like