Professional Documents
Culture Documents
1st Lesson:
Welcome to the Code Avengers Intro to Python! In this pdf you will get those topics that are essential for exams.
Furthermore, please do not forget to study quiz questions that include in 5 th task of 1st ,2nd ,3rd and 4th lesson, I may
ask the same questions or similar ones.
If we want to print more than one line at a time, there are 2 ways we can do it. We can use triple quotes around the
Or we can use a special character called a newline character: \n which will create a new line anywhere you put it in
your text:
We can use some of the symbols we have for math to do some tricks with strings in print
statements:
print("Hello" + "Bob") #prints HelloBob
print("Hello" * 5) #prints HelloHelloHelloHelloHello
As you can see, we can add strings and multiply them.
2nd Lesson:
Variables and input:
We can make our code a lot more fun by asking the user questions and using their
answers. To do this we use an input statement and a thing called a variable to store their
response. A variable is like a named 'container' that stores a value.
The following code asks the user's age and stores it (using =) in a variable called age:
age = input("What is your age?")
We can print variables just like strings and numbers:
print(age)
We don't need " " around a variable name either.
Combine variables and strings: .format()
We can combine values such as variables with a string in a print statement using a function
called format() :
name = input("What is your name?")
age = input("How old are you?")
The values in the format() function are put in the {} placeholders in the order that they're
written.
This is useful when we have more than one value to add in, or if the place we want to insert
a value is in the middle of the string.
Asking for numbers: int()
Sometimes when we ask a question the user's answer is a number.
In this case we have to use a function called int() around our input() when we get their
response:
Variables are called that because their value can vary or change throughout a program.
Ex/ number = 10
number = number + 5 #Now number is 15
Note/ the codes that run in 1st & 2nd lesson are run in a sequence way.
3rd Lesson:
Making decisions:
This is called selection in programming - making decisions about what should happen
based on conditions.
This is like, if someone's parents say: we'll get you that new phone you want IF you tidy your
room. If the condition of tidying their room is True then they will get the phone, otherwise
they won't, in programming this is done with an if statement.
== is equal to
!= not equal to
The else branch says if it's anything other than the thing we checked for in the if, then do
this example:
if fave_food == "pizza":
print("Yum!")
else:
print("Yuck")
Note: For code to be inside a branch it has to be indented or tabbed in one tab.
Note/ the codes that run in 3rd lesson are run in a selection way.
4th Lesson:
Sometimes we want to do something more than once in a row in a program. We could write
the code several times, but we also have structures that are also known as loops. Loops
repeat code for us, We will look at 2 types of loops: for loops and while loops
print(i)
A for loop repeats the block of code inside it a set number of times.
i is the built-in counter that keeps track of how many times the loop has repeated.
We can customize a for loop in several ways, including making it start and stop
at different numbers, for example:
We can also print anything inside the loop, not just the numbers themselves. For example:
for i in range(5):
print("Hello!") # This will print Hello! 5 times on separate lines.
BUT In a while loop, instead of saying how many times we want the loop to repeat, we
use a condition -- just like in an if statement. The loop will run until the condition is met:
while i <= 5:
print(i)
This is the basic structure of a while loop, but we have to write a bit more code for it to
work because it doesn't have a built-in counter like the for loop, for example:
i = 0
while i <= 5:
i += 1 Note/ the codes that run in 4th lesson are run in loops
Review questions (Python):
1- Which of these is the Python command that outputs text to the
console?
print( )
2- Which is the correct symbol for multiplication in Python?
*
3- Which of these print statements will print out the number 7?
Print(3 + 8 – 4)
4- Which of these is the special character for putting text onto a new
line?
\n
5- Which of these is the best description of a variable?
6- The word Sally is saved in a variable called name. Which of these print
statements will correctly print out: That snake is named Sally
print("That snake is named {}".format (name))
:
10- What is the keyword used for making a second branch in an if
statement that will run whenever the if condition is False?
Else
==
12- How do you know which bit of code is inside an if statement?
It is indented
13- How many times will "Ni!" be printed out with the following loop?
1. for i in range(4):
2. print("Ni!")
4
14- What is the last number that the following loop will print out?
1. for i in range(6, 32):
2. print(i)
31
15- How many times will "Hello!" be printed out with the following loop?
1. i = 0
2. while i < 5:
3. print("Hello!") i
+= 1
5
4.
16- What is the last number that the following loop will print out?
1. i = 0
2. while i < 25:
3. print(i)
4. i += 1
24