Lab3 Week5and6
Lab3 Week5and6
1. Print your name to the In Python, the print function is used to print text-based information to the
console. console. A typical function call would look like this:
Use this syntax to print out your name. Use Quotations marks.
2. Print out your city of We could print out all of our program's output on a single line, but that would
birth. not be very easy to read. It’s typical to have a separate print () function call for
each thing we want to print out.
After your first line of code (from above), print out your city of birth.
3. Print the results of When you want Python to print something that is not a literal sequence of
mathematical expressions. characters, you do not use the quotation marks.
print (3 * 4 * 5)
Add another line of code to print out the number of seconds in the current
month. Have Python do the multiplication, using the example as a guide.
4. Print the results of Use the // operator for integer division and % for the integer remainder.
integer division and
remainder. print (44 // 3)
print (44 % 3)
Add code to calculate how many full dozens are made by 403 eggs, and how
many eggs you have remaining.
4. Place variables inside a You can assign a value to a variable. Here are two examples, one that is an
print() function. integer and one that is a string.
age = 20
job_title = 'cashier'
print (age)
print (job_title)
Think of other variables. Give them meaningful names. Use the = operator to
store values in the variables. Print out the value of the variables.
Python Lab3 (b): Input and output
1. Ask the user to enter In Python, the input() function is used to accept input. The result is stored as
their name. Then say hello a string type.
and repeat their name.
x = input ('what is your name?')
print ('Hello ' + name)
2. Ask the user to enter To be able to add 5 to the user input, it has to be converted to an integer.
their age. Print out their age
5 years from now. x = int(input ('what is your age?')
print (x + 5)
3. Ask the user to enter any x = int (input ('enter first integer: '))
two integers. Print out their y = int (input ('enter second integer: '))
addition, subtraction, print ('Addition ',x + y)
multiplication, and division. print ('Subtraction ', x - y)
print ('Multiplication ',x * y)
print ('Division ',x // y)
1. Write an if statement to
determine if a number is
divisible by 5.
Ask the user to enter a number. Determine if the number is divisible by 5.
2. Write an if/else statement. An else statement can follow an if statement. The code in an else
statement is executed if the expression in the if statement is false.
1. The while statement allows the commands with the enter an integer: 3
body of the loop to be repeated, as long as a certain 3
condition is true. 2
1
Ask the user to enter an integer. Store the value in x. End loop!!
Then add this code which counts down.
2. Modify the loop to print out whether a number is even enter an integer: 3
or odd. An even number is divisible by 2, and an odd 3 is odd
number is not. 2 is even
1 is odd
End loop!!