Python # 7 - Python Examples - OnlineTutorialHub

Search This Blog

Theme images by Storman. Powered by Blogger.

About Fast Edition

Popular Posts

Pages

Services

Blog Archive

Wednesday 15 November 2017

Python # 7 - Python Examples


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

OnlineTutorialHub, OTH, Python, Python Examples

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.
Python 3.6.2 Example-
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

OnlineTutorialHub, OTH, Python, Python Examples


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

OnlineTutorialHub, OTH, Python, Python Examples
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.



1 on: "Python # 7 - Python Examples"