Practice 1_part 2
Practice 1_part 2
me="Ann“
age=16
weight=45.5
print("it's %s, %d old. Weight %.2f"
%(me,age,weight))
Result:
it's Ann, 16 old. Weight 45.50
Example 2
Example 3
Task 4
E
x
a Print the sum of the current number and the previous number
m
p Write a program to iterate the first 10 numbers, and in each
l iteration, print the sum of the current and previous number.
4
➢ Create a variable called previous_num and assign it to 0
➢ Iterate the first 10 numbers one by one using for loop and range() function
➢ Next, display the current number (i), previous number, and the addition of
both numbers in each iteration of the loop. Finally, change the value of the
previous number to the current number ( previous_num = i).
EXAMPLE
SOLUTION
T Print the sum of the current number and
a the previous number
s
k Write a program to iterate numbers from 11
to 18, and in each iteration, print the sum of
the current and previous number.
4
E Print characters from a string that are
x present at an even index number
a
m
Write a program to accept a string from the
p
user and display characters that are present
l at an even index number.
e
For example, str = "python" so you should
display ‘p’,’t’,’o’.
5
➢ Use the Python input() function to accept a string from a user.
➢ Calculate the length of the string using the len() function
➢ Next, iterate each character of a string using for loop and range() function.
➢ Use start = 0, stop = len(s)-1, and step =2. the step is 2 because we want only
even index numbers
EXAMPLE ➢ In each iteration of a loop, use s[i] to print characters present at the current
even index number
SOLUTION
result:
T Print characters from a string that are
present at an even index number
a
s
k Write a program to accept a string from the
user and display characters that are present
at an even index number.
5 For example, str = “solution”.
T Print characters from a string that are
present at an even index number
a
s
k Write a program to accept a string from the
user and display characters that are present
at an odd index number.
6 For example, str = “programming”.
E The while loop statement repeatedly executes a code block
x while a particular condition is true.
a
m count = 1
6
E 1 Print First 10 natural numbers using while loop
x
a
Example:
m # program 1: Print natural numbers 90<x<100
p i = 90
while i <= 100:
l print(i)
e i += 1
7
2 Print the following pattern
Write a program to print the following number pattern using a
loop.
E
x Solution:
a
m
p
l
e
8
3 Calculate the sum of all numbers from 1 to a given number
Write a program to accept a number from a user and calculate the sum of all numbers from 1 to a given number
For example, if the user entered 10 the output should be 55 (1+2+3+4+5+6+7+8+9+10)
Task 7
Solve using for loop and range() function
Write a program to print multiplication table of a given number
For example, num = 2 so the output should be
T • Set n =2
a • Use for loop to iterate the first 10 numbers
• In each iteration, multiply 2 by the current number (p = n*i)
s
• Print p
k
Expected output:
8