Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views6 pages

Getting Started With Python - Part 5

Uploaded by

diyaelma17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Getting Started With Python - Part 5

Uploaded by

diyaelma17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Getting Started with Python

GETTING STARTED WITH PYTHON


Part 5 – Input and Output using input() and print() functions

Simple Input using input() function


Python provides a built-in function input() which is used to accept input from the user. The
syntax of input() function is :
<variable name> = input(<prompt string>)
For eg. a=input('Enter a number :')

When the above statement is executed, the prompt string ‘Enter a number’ will be
displayed, and whatever value is entered by the user will be assigned to the variable a.
>>> age=input('Enter your age :')
Enter your age :15

>>>age
'15'
The input() function will convert whatever value is entered into a string, and assign it to the
variable. In the above statement, age is a string variable and its value is displayed as ‘15’.
These string values cannot be used for numerical calculations. In order to convert these
string values into numerical values, Python provides two functions :
1. int() – this function converts a string value to integer value. The string value should
be of an integer format, in order to successfully convert to integer type.
For eg. >>>type(age)
<class 'str'>
>>>a=int(age)
>>>type(a)
<class 'int'>
2. float() – this function converts a string value to float or real value. The string value
should be of a valid float format, in order to successfully convert to float type.
For eg. >>> price=input('Enter the unit price :')
Enter the unit price :25.50

>>>type(price)
<class 'str'>
>>>p=float(price)
>>>type(p)
<class 'float'>

Computer Science with Python Class XI Page | 1 © Reeba John, Computer PGT, MTPS
Getting Started with Python

The input() function and int() or float() functions can be combined in a single statement, in
order to accept a value and convert to numerical type.
>>> age=int(input('Enter your age :'))
Enter your age :18
>>>type(age)
<class 'int'>
>>> price= float(input('Enter the unit price :'))
Enter the unit price :175.2
>>>type(price)
<class 'float'>

Formatting output using the print() function


We have already seen how the print() function is used to display a message or a result on
the output window. You can also use print() function to format the output in different ways.
The complete syntax of the print() function is :
print(<objects> [,sep=' '] [,end='\n'])
The objects argument specifies the various objects to be printed. If more than one object is
given, they have to be separated using a comma.
For eg.
>>>print('Hello','World')
Hello World
>>>x,y,z=5,10,15
>>>print(x,y,z)
5 10 15
The sep argument specifies what is the separator character to be printed between the given
objects. It is an optional argument and by default sep is a space, and hence a space is
printed between the objects printed. The sep argument can be set to any other character.
For eg.
>>>print('Hello','World',sep='-')
Hello-World
>>>x,y,z=5,10,15
>>>print(x,y,z,sep='*')
5*10*15
The end argument specifies what is the character printed after each line of print. It is also
an optional argument and by default end is ‘\n’ the newline character. Hence each print()
statement will print each output on a new line. The end argument can be set to any
character, and in such a case, the next print() statement will print the output on the same
line.
For eg.
print('Hello','World', sep='-',end='$')
Hello-World$

Computer Science with Python Class XI Page | 2 © Reeba John, Computer PGT, MTPS
Getting Started with Python

The following code has to be executed as a script or a program in order to see the effect.
print('Hello','World', sep='-',end='$')
print('Welcome','to','programming', sep='*',end='%')

The output will be :


Hello-World$Welcome*to*programming%

Another eg.
print('Jack','and','Jill', end='$')
print('went','up','the','hill', sep='#')

The output will be :


Jack and Jill$went#up#the#hill

ASSIGNMENT 1
Q.1. Write the statements to perform the following operations :
1. Accept a student’s roll number
2. Accept the name of the student
3. Accept the total marks of the student

Q.2. Predict the output of the following code :


print('The','world','of','programming',sep='-*-',end='!!')
print('is fun and exciting',sep='&&&',end='@@@')

ASSIGNMENT 2

Q.1. Write a program to accept the name, class and house of a student and print each data
with a proper heading on separate lines. Also print each data separated by ‘<-->’.

Q.2. Write a program to accept a number and print its first five multiples.

Q.3. Write a program to accept a number and print its square and its cube.

Q.4. Write a program to accept two numbers and print their sum, difference and product.

Q.5. Write a program to accept three marks and print the average marks.

Q.6. Write a program to accept principal amount, rate and time and calculate simple
interest.

Computer Science with Python Class XI Page | 3 © Reeba John, Computer PGT, MTPS
Getting Started with Python

Elements of a Python program


Before we enter the world of programming, let us first understand the elements of a Python
program. Let us first look at an example of a Python program and then identify the various
elements in it.

A Python program normally consists of the following elements or components :


1. Comments – Comments are the documentation added into a program to make it
more readable and understandable to the programmer. The Python interpreter
ignores all comments written in a program. But they are an important part of any
program and are placed in a program to give information about its logic and
functionality. Comments are displayed in red colour in Python IDLE window.

Comments in Python can be written in two ways :


a. Single line comments – Any line that starts with the hash symbol # is a single line
comment. Single line comments can also be written in two ways :
i. Full line comment – If a line starts with a #, then it is a full line comment,
as the entire contents of the line is treated as a comment.
For eg. #accept input for length and breadth
ii. Inline comment - If the # symbol is place in the middle of a line, it is an
inline comment, and only the text that follows # is treated as a comment.
For eg. A=L*B #calculate area
P=2*(L+B) #calculate area

b. Multi line comments – Comments that are written on more than one line are
multi line comments, and it can also be written in two ways :
i. By starting each line with the hash symbol :
For eg. #display both the

Computer Science with Python Class XI Page | 4 © Reeba John, Computer PGT, MTPS
Getting Started with Python

#area and perimeter


#of the rectangle
ii. By enclosing in triple quotes :
For eg. ''' This is a program to accept
the length and breadth of a rectangle
and find the area and perimeter '''
Comments enclosed in triple quotes are also known as docstrings, and they are
normally placed at the beginning of the program to identify what the program is
about. Docstrings are strings enclosed in either triple single quotes or triple
double quotes, and they are displayed in green colour in Python IDLE, like any
other string.

2. Expressions – An expression is any legal combination of variables, operators and


literals. An expression is evaluated it produces a value.
For eg. L*B
2*(L+B)
3. Statements – A statement is a programming instruction that does something or
some action takes place. A statement is executed by the Python interpreter and it
need not always result in a value. In the above program, the following lines are all
statements.
For eg. L=int(input("Enter Length:"))
B=int(input("Enter Breadth:"))
A=L*B
P=2*(L+B)
print("Area = ",A)
print("Perimeter = ",P)
4. Blocks and Indentation – A group of statements that are part of another statement is
called a block or code-block. Python uses indentation to create a block, which means
that these statements are moved a tab space or 4 spaces ahead under the statement
it is defined. Hence the main statement is at one level, and the block of statements
under it are at another level. A block is created under a statement that ends with a
colon ( : ).
For eg. if x>0 :
print("It is a positive number"))
x=x*10
Here the if statement is the main statement and it ends with a colon, and the two
statements under it make the block of statements under the if statement. These two
statements are indented to indicate they are part of the block under if statement.
5. Functions – A function is an independent unit of code that has a name, and a block
of statements under it which forms the function block or function body. A function
can be reused and executed as many times as needed, by calling the function, which
executes the statements in the function block. We can create our own functions in a

Computer Science with Python Class XI Page | 5 © Reeba John, Computer PGT, MTPS
Getting Started with Python

program, and Python has a library of built-in functions, which can also be used in
your programs, like print(), input(), int(), type(), etc.

6. Whitespaces – Whitespaces like space, tab space, blank lines are needed in a
program to make it more readable. A space can be placed before and after
operators, and a blank line after a particular section of code.

Note : Colour theme used in Python IDLE


The Python IDLE window has a colour theme to indicate various elements in the code. Some
of the colours used are :

Computer Science with Python Class XI Page | 6 © Reeba John, Computer PGT, MTPS

You might also like