1) Print “Hello world”
Step 1 - Open the python compiler
([Link]
Step 2 - Delete everything that is already written on the compiler.
Step 3 - Now type the following in the compiler exactly as it is:
Python
print (“Hello world”)
Here, whatever you want the compiler to produce as it is, it should be inside the bracket and
inside the double inverted commas/ quotes. Whatever is outside the brackets, it is what you
want the compiler to do for you.
Step 4 - Click on the blue run button above your code. It will show the output of your code on
the right side, which is called the shell.
2) Write a program to read a number
Step 1 - Open the python compiler
Step 2 - Delete everything that is already written on the compiler.
Step 3 - Now type the following in the compiler exactly as it is:
Python
x = input (“Enter a number”)
Here what you put in the bracket and quotes, will show up as it is on the shell. Once it shows up
on the shell, click on the shell side, and type any number you want. Why you do this is so that
the program can understand the number you have written.
x = input code helps the program to remember what number you typed in the [Link] you don’t
use the x = input code, the program later on, will not remember your number and will not
understand what to do with the number you have typed.
3) Write a program to read and display your name
Step 1 - Open the python compiler
Step 2 - Delete everything that is already written on the compiler.
Step 3 - Now type the following in the compiler exactly as it is:
Python
x = input ("Enter your name")
print ("My name is", x)
Step 4 - Make sure you have put a comma after the sentence inside the quotes. Click on the
run button. Type your name in the shell, then hit enter. It will display your name with the
sentence you have given inside the quotes, and the name that you typed in the shell.
4) Write a programme to read two numbers and find the greater
number
Step 1 - Open the python compiler
Step 2 - Delete everything that is already written on the compiler.
Step 3 - Now type the following in the compiler exactly as it is:
Python
x = int (input ("Please type a number for x"))
y = int (input ("Please type a number for y"))
if (x>y):
print (x, "is greater than", y)
else:
print (y, "is greater than", x)
Step 4 - Pay attention to the following important elements:
● Use int() to convert the input to integers (whole numbers). That means we write int,
which makes sure the program knows that the character you enter in the shell is a
number.
● Look carefully at where brackets are used for both the inputs, there are two brackets.
One after int, and one after the input which is already inside a bracket.
● The if and else statement is followed by a colon (:) and the next line is indented.
● When you are using double inverted commas to print something, and you also want the
program to read the value of the variable, separate them by commas (as seen in if and
else statements).
Step 5 - Click on the "Run" button. This will execute your code.
Step 6 - The program will ask you to enter two numbers on the shell. Type in your numbers by
typing one number, pressing enter, typing the second number and pressing enter again. The
program will compare them and print a message to tell you which number is greater.
5) Write a program to print number from 0 to 5
Step 1 - Open the python compiler
Step 2 - Delete everything that is already written on the compiler.
Step 3 - Now type the following in the compiler exactly as it is:
Python
x = range (6)
for i in x :
print (i)
Step 4 - Pay attention to the following important elements:
● range(6) creates a sequence of numbers from 0 to 5. The number after range is also
typed in brackets. The for is followed by a colon (:).
● The print(i) statement is also indented within the loop. It prints the current value of i
during each type the code repeats, which is the feature/function of i or loops.
Step 5 - Click on the "Run" button.
Step 6 - The program will run, and you'll see the numbers 0 to 5 printed in sequence on the
screen.
Note - The variable i is used to represent each number in the sequence from 0 to 5. The range
always takes one lesser than the number you have typed. So if you have typed 6 in the brackets
after range, it will only print the number from 0 to 5. If you type 5, it will only print number from 0
to 4.
6) Write a program to add number in range 1 to 5
Step 1 - Open the python compiler
Step 2 - Delete everything that is already written on the compiler.
Step 3 - Now type the following in the compiler exactly as it is:
Python
x = 1
y = 5
print ("The sum of 1 to 5 are")
sum = 0
for i in range (x,y+1):
sum = sum + (i)
print (sum)
Step 4 - Pay attention to the following important elements:
● Variables 'x' and 'y' store the starting and ending values for a range of numbers.
● The variable 'sum' is initialized to 0 at the beginning because it will be used to
accumulate the sum of numbers.
● The for loop starts with range(x, y + 1), which includes numbers from 'x' to 'y' (inclusive).
● Inside the loop, sum is updated by adding the current value of 'i' to it.
● The print(sum) statement prints the cumulative sum during each iteration of the loop.
Step 5 - Click on the "Run" button.
Step 6 - The program will run, and you'll see a sequence of numbers printed on the shell,
representing the total sum from 1 to 5. Which means the program did 1 + 2 + 3 + 4 + 5 for you.
Note - The 'sum' variable is used to add the sum of numbers from 'x' to 'y'. It starts at 0 because
it's empty at the beginning, and we want to add numbers to it as the loop progresses.