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

Programming - Week 3 Part1

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

Programming - Week 3 Part1

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

Fundamentals of

Programming
Week 3 - Functions, Introduction
to Loops and Lists

30.09.2024 | DOT 1003 | Mahmut Can Kovan


Agenda
● What is Function
● Method vs. Function. What is the difference?
● What is Loops
● While
● For

55
What is Function?
● a block of code which is executed when called
● int(), float(), print(), input() is an example of built-in,
functions which were used in this course
● We can create our own functions
● We can call functions as much as we want

56
Method vs. Function. What is the difference?
● Method is almost same thing as function
● if we define a function in a class it is called a method?
● Methods are attached to the object they are called on
● Syntax is slightly different

57
How to Create a Function
def my_message():
print("No gods or kings. Only man.")

my_message()

● we need >No gods or kings. Only man.

○ a keyword called “def”


○ function name def my_message(first_arg):
print(f"No {first_arg} or kings. Only man.")
● We could add
○ argument(s) my_message("GlaDOS")

■ for multiple argument, use “,” to


>No GlaDOS or kings. Only man.
separate them
○ return parameter
def my_message(first_arg):
return f"No {first_arg} or kings. Only man."
def my_message():
print("No gods or kings. Only man.") print(my_message("GlaDOS"))

> >No GlaDOS or kings. Only man.

58
Programming Task 21
● Please create a function which takes one argument as a name, no
return parameter and print this argument like below using f-string

>Please input an argument: Gordon


>Hello Gordon

59
Programming Task 22
● Please create a function which takes one argument as a name, has
return parameter and print this argument like below using f-string

>Please input an argument: Gordon


>Hello Gordon

60
Programming Task 23
● Please create a function named sum which takes two argument and
return of their sum

>5 #define a function named sum here

#TODO

#main code
print(sum(3,2))

61
Programming Task 24
● Find the problem and fix it.

>Hi Gordon Freeman name = "Gordon Freeman"


>Hi Gordon Freeman
def greeting(input_name):
print(f"Hi {name}")

greeting("Andrew Ryan")
greeting("Gordon Freeman")

62
Programming Task 25
● Please create a function which takes three arguments as an integer
and print out the arithmetic mean of them

>2.0 #define a function named sum here

#TODO

#main code
mean(3,2,1)

63
What is Loop?
● a block of code which is iterate finitely or indefinitely
● We call loops as one of fundamental control structures
● The code block inside loop would iterate (repeat) as much as we want
● Loops are easy to learn but hard to master structures
● Quality of your codes in terms of “time complexity” is depend on your
loops (most of the time)

64
While Loop
print("Please type in a number as much as you want")
print("Type -1 to quit")

my_flag = True

while my_flag:
● Please don’t use break and my_input = input("Input: ")

continue!
print(f"Your input is: {my_input}")

Syntax is easy and works like


if my_input == "-1": #This is Exit Condition
● my_flag = False
conditional statements print("Bye!")

● Loop will stop iterating when the


while statement is False >Please type in a number as much as you want
>Type -1 to quit
● You must put exit condition at >Input: 1
>Your input is 1
some point in the code, otherwise >Input: Mahmut
>Your input is Mahmut
It would iterate forever >Input: -1
>Your input is -1
● >Bye!

65
Programming Task 26
● Please create program which ask name and if the name is correct, It
will print “You're goddamn right.” and finish.

>Say my name: Mahmut


>Say my name: Agent 47
>Say my name: Kratos
>Say my name: Geodude
>Say my name: Heisenberg
>You're goddamn right.

66
Programming Task 27
● Please create program which asks the user for a password. After that,
program ask password again to verification. If correct, finish.
Otherwise ask again and again.

>Enter your password: myPassword


>Enter again: my_password
>They are not same.
>Enter again: MyPassword
>They are not same.
>Enter again: myPassword
>Your password matches and account created successfully

67
Programming Task 28
● Please create a countdown which ask user for start number and
countdown to zero.
● Print every step. When counter reach zero, print Kaboom and exit.

>Please enter a number: 3


>3
>2
>1
>Kaboom

68
Programming Task 29
● Please create program which ask the user password for 3 times
● If correct password entered, print “Welcome” and finish. Otherwise
print “Try Again” and ask password again
● If user couldn’t enter correct password after 3 trial, print “Incorrect
Password. Exterminate...” and finish.
>Password: 123456
>Try Again
>Password: 654321
>Try Again
>Password: 314159
>Welcome

69
Programming Task 30 - Assignment 1
● Please create program which ask integers. Whenever user enters 0,
program finish and show the results
○ Count how many integers does user enter and print
○ Calculate the sum of these numbers and print
○ Calculate arithmetic mean of these numbers and print
○ Calculate how many inputs are odd and even seperately and print

>dumb calculator v0.1 If you want to exit, enter 0


>please enter a number: 3
>please enter a number: 5
>please enter a number: 19
>please enter a number: 90
>please enter a number: 0
>Total Number: 4
>Sum: 117
>Mean: 29,25
>Odd: 3 Even: 1 70

You might also like