Python Examples:-
Python code straightforward and simple to run. Here is a basic Python code that will print "Welcome to Python".Python examples are given below.
Example 1:
>>> a="Welcome To Python">>> print a
Output-
Welcome To Python
Explanation:
- We are using IDLE to write the Python code. Detail clarification to run code in IDLE is given in my previous post (Execution of Python).
- A variable is defined named "a" which holds "Welcome To Python".
- "print" statement is used to print the content. subsequently "print a" statement will print the content of the variable. In this way, the output "Welcome To Python" is produced.
In python 3x versions, you need to add parenthesis () in a string code to print it.
Example 2:
1) Python program to add two numbers -Code :
>>> num1 = 2.5
>>> num2 = 6.5
# Add two numbers
>>> sum = num1 + num2
# Display the sum
>>> print ( sum )
Output–
9.0
2) Python program to add two numbers -
Code :
# Store input numbers >>> num1 = input ('Enter first number: ')
>>> num2 = input ('Enter second number: ')
# Add two numbers
>>> sum = float (num1) + float (num2)
# Display the sum
>>> print ('Sum of {0} and {1} is {2}'.format (num1, num2, sum))
Output–
Enter first number: 7.6
Enter second number: 3.5
The sum of 7.6 and 3.5 is 11.1
Explanation :
- In this program, we requested that user to enter two numbers and this program displays the sum of two numbers entered by user.
- We use the built-in function input() to take the input. input() returns a string, so we convert it into number using the float() function.
Great Article
ReplyDeleteFinal Year Projects for CSE in Python
FInal Year Project Centers in Chennai
JavaScript Training in Chennai
JavaScript Training in
Chennai