Sinem E - Y9-Python Recap
Sinem E - Y9-Python Recap
We also learnt that if we add int() before input(), we can convert numeric
letters to actual numbers.
2. print() allows to display data to the user on the screen. We can use a pair
of quotations to output a statement, or we can call variables in the
brackets to output the value stored in it.
Example: name = input(“Enter your name:”)
print(“Hello,” , name)
3. For the CPU to be able to process input and output data, we must create
variables in the memory and store values in them.
Example: Age = int(input(“How old are you:”)
print(“You’ll be 100 in” , 100-Age, “years!”)
Age is a variable in which we store the value that the user entered for us.
Without storing the value, we would not be able to generate the print()
message which has a calculation in the middle of statements in
quotations.
4. if <condition> … elif <condition> … else:
we can get the computer to select what to do using a SELECTION block.
There can be as many conditions as necessary and if a condition is not
met, the computer ignores the code beneath it.
Example:
Age = int(input(“Enter your age:”))
if Age < 12:
print(“Primary Student”)
elif 12 <= Age < 14:
print(“Key Stage 3”)
elif 14<= Age < 16:
print(“Key Stage 4”)
else:
print(“Key Stage 5”)
5. for <counter> in range ( a , b):
we can get the computer to repeat a block of code multiple times by
using for loop.
We must choose a name for our counter.
We must set up a lower bound, and an upper bound in the brackets after
range.
if lower bound is not set, the computer automatically starts from 0.
Upper bound is not counted in loop repetitions. In fact, it breaks out of
the loop once the counter reaches the upper bound.
Example:
x = int(input(“How many students?”))
class_average = 0
mark = 0
class_ total = 0
for count in range (x):
mark = int(input(“ Enter the student´s mark:”))
class_total = class_total + mark
class_average = class_total / x
print(“The average mark of this class is:”, class_average
c. Which line in the above code determines whether the input value is
divisible by 2?
Task 2:
a. Type the following code in your code editor and add a screenshot of
the code and its execution.
b. Explain the purpose of this program in the box below.
c. You just learnt a new built-in function in python. len() counts the
number of characters inside a text/string. What would be printed to
the screen if the following code would be executed? DO NOT type in
your code editor. Use your brain.
L = len(“HELLO WORLD!”)
print(L)
Answer: ………………………
Task 3:
Using all we have practised so far, write a program that receives an integer
number from the user (for example: 5) and a letter from the user (for example
“A”). Next, the program prints the letter as many times as the integer number.
In the case of example, A would be printed 5 times.
Add the screenshot of the code and its execution here.
Task 4:
Write a program that receives 10 numbers from the user. It must count the
negative numbers and the positive numbers. Zero is not counted. In the end,
the program must display the count of positive numbers and their sum, the
count of negative numbers and their sum, and the average of all the 10
numbers.
Add the screenshot of the code and its execution here.
Extension Task:
There is a built-in function in Python that receives ASCII value of a letter
and generates the letter.
There is another built-in function which does the reverse. It takes in a
letter and generates the ASCII value.
Go on Google and find them.
a. Write a program that receives the user’s name and outputs the
sum of ASCII values of the letters in the name.
b. Write a program that receives an integer value from the user in
range of 65 and 91 inclusive. Then it outputs the corresponding
letters in a sequence.