Python Lecture
Python Lecture
A program will ask the user to enter a temperature and convert it to Fahrenheit.
Sample output:
Source code:
The input function’s job is to ask the user to type something in and to capture what the user types.
The part in quotes is the prompt that the user sees. It is called a string and it will appear to the program’s user exactly as it
appears in the code itself.
The eval function is something we use it when we’re getting numerical input.
We need to give a name to the value that the user enters so that the program can remember it and use it in the second line. The
name we use is temp and we use the equals sign to assign the user’s value to temp.
The second line uses the print function to print out the conversion. The part in quotes is another string and will appear to your
program’s user exactly as it appears in quotes here. The second argument to the print function is the calculation. Python will do
the calculation and print out the numerical result.
Write a program that computes the average
of two numbers that the user enters:
Sample output:
Source code:
GENEVEV G. REYES
Subject Teacher
Review
• What are the two(2) optional arguments to the print function?
• This print argument can use to keep the print function from
advancing to the next line.
• This print argument can use to change that space to something
else.
• What is the output of the program below?
print ('The sum of 5 and 1 is', 5+1, '.', sep=':')
a.
b.
Prep Task:
Create a program that will print Hello ten times:
Sample output:
For loops
Example 1. The following program will print Hello ten times:
The structure of a for loop is as follows:
for variable name in range( number of times to repeat ) :
statements to be repeated
The syntax is important here. The word for must be in lowercase, the
first line must end with a colon, and the statements to be repeated
must be indented. Indentation is used to tell Python which statements
will be repeated.
Task 1
Create a program that prints your name five(5) times
using the for loop.
Task 2
Create a program that prints I LOVE YOU. five(5) times
in one line using the for loop.
Sample output:
Example 2
• Create a program that asks the user for a number and prints
its square, then asks for another number and prints its
square, etc. It does this three times and then prints that the
loop is done.
Sample output
Example 3
What is the output of the program below?
print(‘A’)
print('B’)
for i in range(5):
print('C’)
print('D’)
print('E')
Example 4
• Write a program that outputs like this:
The loop variable
• It’s a convention in programming to use the letters i, j, and k for loop
variables, unless there’s a good reason to give the variable a more
descriptive name.
The range function
• The value we put in the range function determines how many times
we will loop.
• The way range works is it produces a list of numbers from zero to the
value minus one. For instance, range(5) produces five values: 0, 1, 2,
3, and 4.
• If we want the list of values to start at a value other than 0, we can
do that by specifying the starting value.
• The statement range(1,5) will produce the list 1, 2, 3, 4. This brings up one
quirk of the range function—it stops one short of where we think it should.
• If we wanted the list to contain the numbers 1 through 5 (including 5), then
we would have to do range(1,6).
• Another thing we can do is to get the list of values to go up by more
than one at a time. To do this, we can specify an optional step as the
third argument. The statement range(1,10,2) will step through the list
by twos, producing 1, 3, 5, 7, 9.
• To get the list of values to go backwards, we can use a step of-1. For
instance, range(5,1,-1) will produce the values 5, 4, 3, 2, in that order.
(Note that the range function stops one short of the ending value 1).
Here are a few more examples:
Write a program that outputs below.
Source Code:
for i in range(5,0,-1):
print(i, end=' ‘)
print('Blast off!!')
A Trickier Example
The program below prints a rectangle of stars that is 4 rows tall and 6
rows wide.
Output:
for i in range(4):
print('*'*(i+1))
The key is to change the 6 to i+1. Each time through the loop the
program will now print i+1 stars instead of 6 stars. The loop counter
variable i runs through the values 0, 1, 2, and 3. Using it allows us to
vary the number of stars.
Exercises
1.A. Write a program that outputs 100 lines, numbered 1 to 100, each
with your name on it. The output should look like the output
below.
1.B. Write a program that outputs 100 lines, numbered 1 to 100, each
with the name you entered. See the sample output below.
2. Write a program that asks the user for their name and how many
times to print it. The pro gram should print out the user’s name the
specified number of times.
3. Write a program that uses a for loop to print the numbers 8, 11, 14,
17, 20, ..., 83, 86, 89.
4. Write a program that uses a for loop to print the numbers 100, 98,
96, ..., 4, 2.
1.
2.
3.
4.
Math Operators
• Here is a list of the common operators in Python:
• Exponentiation - Python uses ** for exponentiation. The
caret, ^, is used for something else.
Example:
print (5**2)
answer: 25