- iterations / loops
Burkay Genç, Ahmet Selman Bozkır, and Selma Dilek
22/03/2023
return
vs print
## Choose a number between 1 and 100! ## Is it 50 (Y, H or L): H ## Is it 75 (Y, H or L): H ## Is it 87 (Y, H or L): L ## Is it 81 (Y, H or L): L ## Is it 78 (Y, H or L): H ## Is it 79 (Y, H or L): Y ## I guessed your number!!!
print("Choose a number between 1 and 100!") low = 1 high = 100 while high >= low: guess = int((high + low) / 2) response = input("Is it " + str(guess) + " (Y, H or L): ") if response == "Y": print("I guessed your number!!!") break elif response == "H": low = guess else: high = guess
def is_even(i): """ Input: i, a positive int Returns True if i is even, otherwise False """ print("inside function is_even()") return i%2 == 0
print("some code...") is_even(3) print("some other code ...")
## some code... ## inside function is_even() ## False ## some other code ...
def square(i): return i**2
square(2)
## 4
square(-3)
## 9
square(0)
## 0
def sum_of_two(i, j): return i + j
sum_of_two(3, 5)
## 8
sum_of_two(-2, 4)
## 2
def is_prime(number): prime = True for i in range(2, int(number / 2)): if number % i == 0: prime = False break return prime
is_prime(7)
## True
is_prime(10)
## False
is_prime(79)
## True
def foo(x): x = 2 * x print(x) x = 5 foo(x)
## 10
print(x)
## 5
def foo(x): x = 2 * x print(x) i = 5 foo(i)
## 10
print(i)
## 5
return
STATEMENTdef is_even(i): """ Input: i, a positive int Does not return anything """ i%2 == 0 print(is_even(5))
## None
return
vs. print
def foo(x): print (x) print (i) i = 5 foo(2 * i)
## 10 ## 5
def foo(x): i = 10 # i is re-assigned in function scope print (i) i = 5 foo(i)
## 10
print(i) # actual i keeps its original value
## 5
global
(advanced topic)def foo(x): global i # i is defined in the global scope i = 10 # the global i variable is reassigned print (i) i = 5 foo(i)
## 10
print(i) # the global value has been changed
## 10
def min_val(sequence): curMin = sequence[0] for val in sequence: if val < curMin: curMin = val return curMin min_val([1,2,3,4,5,6,7,-1,-2,-3,-4,-5])
## -5
min_val(["burkay", "ahmet", "ayşe", "hatice", "orkun", "zeynep"])
## 'ahmet'
def reverse_str(s): r = "" for char in s: r = char + r return r reverse_str("burkay genc")
## 'cneg yakrub'
Write a function to convert a given length in centimeters to inches
Write a function that takes three arguments
Write a function to build a pyramid of given height
Write a function to check whether a given substring is found in a given string and returns its index if found, return -1 otherwise.
Write a function that takes two arguments and returns their smallest common multiple.
Write a function that returns the fibonacci number for a given value.
These slides are a direct adaptation of the slides used for MIT 6.0001 course present (as of February 2020) on MIT OCW web site.
Original work by:
Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts Institute of Technology: MIT OpenCourseWare. License: Creative Commons BY-NC-SA.
Adapted by and for:
Asst. Prof. Dr. Burkay Genç. MUH101 Introduction to Programming, Spring 2020. Hacettepe University, Computer Engineering Department.