Example HTML page

First Python program

Here I would like to create a sample python program. So you need to open a new window by clicking ctrl+n and save this as “.py” extension. Ex: “hello.py”.

Now type the following code

print("Welcome to Python")

To execute the above program click on “run” then select “run module”. Otherwise press “F5”.

Welcome to Python

I/O in python

Using print( )

Printing the output will show you the final result of your code. For this Python has built-in function called print( ) to display the output.  We have to notice that there is no semi-colon at the end of statement.

Syntax for print( ) is simple

  1. To display string.

print(“any string”)

Ex:

>>> print("Sudhakar")
Sudhakar
  1. If you want to print the value of a variable, then use

print(variable)

Ex:

>>> s=4
>>> print(s)
4
  1. If you want to display both string and variable separate them by a comma and put them within parenthesis.

print(“any string”,variable)

Ex:

>>> s=4
>>> print(s)
4
>>> print("Sudhakar",s)
Sudhakar 4

Using input( )

Python has a built-in function called input( ).This is used to take the input from the user in the string format. String is nothing but a sequence of characters. We can store that input in a variable. You can print it by using the print( ) function.

Ex

fruit=input("My favourite fruit is: ")
print(fruit)
My favourite fruit is: apple
apple

The syntax for input is quite simple:

  1. Take an input a string from the user.
>>> str=input("Enter a string")
Enter a string Python
  1. If you want to take input an integer value then
>>> var=int(input("Enter a number"))
Enter a number 9

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest