Program to print a table using while loop

In this program, we will print a table using while loop in Python.

Steps to solve the program
  1. Create a count variable and assign its value equal to 1.
  2. While value of the count variable is less than 11 multiply the count variable by 2 and store the value in another variable.
  3. Add 1 to the count variable after each iteration.
  4. Print the output.
				
					count = 1

while count < 11:
    a = count*2
    print(count,"*",2,"=",a)
    count += 1
				
			

Output :

				
					1 * 2 = 2
2 * 2 = 4
3 * 2 = 6
4 * 2 = 8
5 * 2 = 10
6 * 2 = 12
7 * 2 = 14
8 * 2 = 16
9 * 2 = 18
10 * 2 = 20
				
			

print the first 20 natural numbers

Sum of the first 10 natural numbers using the while loop

Program to print the first 20 natural numbers

In this program, we will print the first 20 natural numbers using a while loop.

Steps to solve the program
  1. Create a variable and assign its value equal to 1.
  2. While the value of the variable is less than 21 print the variable.
  3. Add 1 to the count variable after printing a number.
  4. Print the output.
				
					count = 1

while count < 21:
    print(count,end=" ")
    count += 1
				
			

Output :

				
					1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
				
			

print the keys and values of a dictionary

Print the table of a number using a while loop

Print the keys and values of a dictionary

In this program, we will print the keys and values of a dictionary.

Steps to solve the program
  1. Take a Dictionary as input.
  2. Get the keys and values of the dictionary using items().
  3. Use for loop to iterate over keys and values of the Dictionary.
  4. Print the output.
				
					dict1 = {'name':'virat','sports':'cricket'}

for key,val in dict1.items():
    print(key,val)
				
			

Output :

				
					name virat
sports cricket
				
			

print the values of the keys of a dictionary

print the first 20 natural numbers using a while loop

Print the values of the keys of a dictionary

In this program, we will print the values of the keys of a dictionary.

Steps to solve the program
  1. Take a Dictionary as input.
  2. Get the values of the keys of a dictionary using values().
  3. Use for loop to iterate over keys of the Dictionary.
  4. Print the output.
				
					dict1 = {'name':'virat','sports':'cricket'}

for value in dict1.values():
    print(value
				
			

Output :

				
					virat
cricket
				
			

print the keys of a dictionary

print the keys and values of a dictionary

Program to print the keys of a dictionary

In this program, we will print the keys of a dictionary.

Steps to solve the program
  1. Take a Dictionary as input.
  2. Get the keys of the DIctionary using keys().
  3. Use for loop to iterate over keys of the Dictionary.
  4. Print the output.
				
					dict1 = {'name':'virat','sports':'cricket'}

for keys in dict1.keys():
    print(keys)
				
			

Output :

				
					name
sports
				
			

create an empty list and add odd numbers from 1-10

print the values of the keys of a dictionary

Create an empty list and add odd numbers to it.

In this program, we will create an empty list and add odd numbers to it from 1-10.

Steps to solve the program
  1. Create an empty list.
  2. Use for loop to iterate over numbers from 1-10.
  3. During iteration check whether a number is odd or not.
  4. Add those numbers to the empty list.
  5. Print the output.
				
					odd = []

for i in range(1,11):
    if i%2 != 0:
        odd.append(i)
        
print(odd)
				
			

Output :

				
					[1, 3, 5, 7, 9]
				
			

create an empty list and add even numbers from 1-10

print the keys of a dictionary

Create an empty list and add even numbers in it.

In this program, we will create an empty list and add even numbers from 1-10 to it.

Steps to solve the program
  1. Create an empty list.
  2. Use for loop to iterate over numbers from 1 to 10.
  3. During iteration check whether the number is even or not.
  4. If it is an even number then add it to the empty list.
				
					even = []

for i in range(1,11):
    if i%2 == 0:
        even.append(i)
        
print(even)
				
			

Output :

				
					[2, 4, 6, 8, 10]
				
			

print each word in a string on a new line

create an empty list and add odd numbers from 1-10

Print each word on new line from the string.

In this program, we will print each word on new line from the string.

Steps to solve the program
  1. Take a string as input.
  2. Use for loop to iterate over each character from the string.
  3. Print the character on the new line.
				
					str1 = "Sqatools"

for char in str1:
    print(char,end="\n")
				
			

Output :

				
					S
q
a
t
o
o
l
s
				
			

display numbers from a list

create an empty list and add even numbers from 1-10