Python is one of the simplest and easy to learn programming language. In the above example we used a program to print a simple message “I love my Country” using the print () statement. This time we will add two integers and then print the sum of the two integers and will demonstrate that how the program works. Consider the following lines of code:
Suppose you have an integer x and an integer y, x is assigned a value 2 and t is assigned a value 4. Consider another integer named sum that is used to store the resulting value after addition. At the end we will display the sum:
#adding two numbers
x = 2
y = 4
sum = x + y
In the line, we used a comment. The # symbol is used add a comment to a python code. A comment is added to add more description to a program. Comments also make the user understand the code well. The comments in any programming language are ignored and are not compiled or interpreted. Comments are used to add assumptions.
Line 2 is assigning a value to a variable x. A value can be assigned to or stored in a variable. In the above example the integer 2 is stored in the variable. The “=” operator is used to assign a value to a variable.
Line 3 is also assigning a value to a variable y. A value can be assigned to or stored in a variable. In the above example the integer 4 is stored in the variable.
Line 4 is adding the two variables or two integers using the + operator. The result obtained after addition of the two variables is stored in another variable named sum.
In line 5 we used a function that is print () function to print the result of the addition in the output screen. Actually we are printing the variable sum in which the value of addition is stored.
- It is not necessary to use semi colon as a statement terminator at the end of a line like other languages such as C, C++, JAVA, etc.
- To enter a new line press the enter key.
- The curly brackets are not used in Python to represent a block of statements instead indications are used to represent a block of statements.