0% found this document useful (0 votes)
23 views

Lab3 Week5and6

1. The print function is used to print text-based information to the console. Variables and results of mathematical expressions can be printed. 2. The input function accepts user input and stores it as a string or integer variable. User input is used with print statements. 3. Functions are defined using the def keyword and can be called to perform tasks and return values. If/else statements determine program flow. 4. A while loop repeats a block of code as long as a condition is true. It is used to count down from a user-input number and check for even/odd values.

Uploaded by

ziko ALH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lab3 Week5and6

1. The print function is used to print text-based information to the console. Variables and results of mathematical expressions can be printed. 2. The input function accepts user input and stores it as a string or integer variable. User input is used with print statements. 3. Functions are defined using the def keyword and can be called to perform tasks and return values. If/else statements determine program flow. 4. A while loop repeats a block of code as long as a condition is true. It is used to count down from a user-input number and check for even/odd values.

Uploaded by

ziko ALH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Lab 3(a): The print function

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:

print ('Welcome to my program! ' )

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)

Python Lab 3(c): Defining functions

1. Define a function The def command is used to define a function.


that returns the
average of 3 numbers. def average(num1, num2, num3):
return (num1 + num2 + num3)/3

print (average(7, 5, 9))


print (average(6, 6, 7))
2. Move the function print (average(7, 5, 9))
definition after the print print (average(6, 6, 7))
statements.
def average(num1, num2, num3):
return (num1 + num2 + num3)/3
Will this script run? Why do you think this is so?

Python Lab 3(d): if statements with else and elif

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.

number = int (input ('enter any number: '))


if (number % 5 == 0):
print (str(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.

number = int (input ('enter any number: '))


if (number % 5 == 0):
print (str(number) + ' is divisible by 5')
else:
print (str(number) + ' is divisible by 5')

Notice that an else statement does not have a Boolean expression.

Write an additional if/else statement to see if the number is divisible by 2.


Test your code with many different inputs.
Python Lab 3 (e): while loops

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.

x = int (input ('enter an integer: '))


while (x > 0):
print (x)
x=x-1
print ('end loop!!')

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!!

You might also like