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

Print a table of a number using for loop

In this program, we will print a table of a number using for loop.

Steps to solve the program
  1. Take the number 5 as input to print its table.
  2. Create a variable and assign its value equal to 0.
  3. Use a for loop to iterate over 1-10.
  4. Multiply the given number with the loop number and store the result in the created variable.
  5. Print the result after each iteration.
				
					num = 5
a = 0
for i in range(1,11):
    a = i*num
    print(i,"*",num,"=",a)
				
			

Output :

				
					1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
				
			

print the following pattern

first 20 natural numbers using for loop