Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Programming Fundamentals (CS-116L) SSUET/QR/114

LAB # 03

CONSOLE INPUT AND OUTPUT

OBJECTIVE
Taking input from user and controlling output position.

THEORY

Console I/O Functions


The keyboard and visual display unit (VDU) together are called a console. Python
programming language provides many built-in functions to read any given input and to
display data on screen, Console (also called Shell) is basically a command line
interpreter that takes input from the user i.e one command at a time and interprets it. If
it is error free then it runs the command and gives required output otherwise shows the
error message.

Accepting Input from Console


To take input from the user we make use of a built-in function input().

Syntax : input(prompt)

Displaying Input from Console


The print( ) function prints the specified message to the screen, or other standard output
device.

The message can be a string, or any other object, the object will be converted into a
string before written to the screen.

Syntax: print(object(s), separator=separator, end=end, file=file, flush=flush)

Example:
name=input('Please enter your name: "') print("Hello,
" , name , "!")

Output:
>>> %Run task1.py Please enter your
name:ABC Hello, ABC!
>>>

Lab 03: Console Input and Output 1


Programming Fundamentals (CS-116L) SSUET/QR/114

Whatever you enter as input, input function convert it into a string. if you enter an
integer value still input() function convert it into a string. You need to explicitly convert
it into an integer in your code using typecasting.

Example:
# Program to check input
num = input ("Enter number :")
print(num)
name1 = input("Enter name : ") print(name1)

# Printing type of input value


print ("type of number",type(num))
print ("type of name",type(name1))

We can also type cast this input to integer, float or string by specifying the input()
function inside the type.
Typecasting the input to Integer/Float: There might be conditions when you might
require integer input from user/console, the following code takes two
input(integer/float) from console and typecasts them to integer then prints the sum.

Example
# input num1 =
int(input()) num2 =
int(input())

# printing the sum in integer


print(num1 + num2)

Escape Sequence
In Python strings, the backslash "\" is a special character, also called the "escape"
character. An escape sequence is a sequence of characters that does not represent itself
when used inside a character or string literal, but is translated into another character or
a sequence of characters that may be difficult or impossible to represent directly.

Escape Description
Example Output
Sequence
\\ Prints Backslash print ("\\") \
\` Prints single-quote print ("\'") '
\" Pirnts double quote print ("\"") "
print hello world
\n ASCII linefeed ( LF )
("hello\nworld")
ASCII backspace ( BS ) removes previous print ("az" + "\b" +
\b ac
character "c")
\t ASCII horizontal tab (TAB). Prints print ("\t*hello") *hello
Lab 03: Console Input and Output 2
Programming Fundamentals (CS-116L) SSUET/QR/114

TAB

EXERCISE

A. Point out the errors or undefined/missing syntax, if any, in the following python
programs.

1. print("Hello \b World!")

2. first_number = str (input ("Enter first number") )


second_number = str (input ("Enter second number") )
sum = (first_number + second_number)
print("Addition of two number is: ", sum)

3. age = 23
message = "Happy " + age + "rd Birthday!"
print(message)

Correct Code:
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)

Lab 03: Console Input and Output 3


Programming Fundamentals (CS-116L) SSUET/QR/114

B. What would be the output of the following programs:

1. a=5
print("a =", a, sep='0', end=',')

2. name = input("Enter Employee Name")


salary = input("Enter salary")
company = input ("Enter Company name")
print("Printing Employee Details")
print ("Name", "Salary", "Company")
print (name, salary, company)

Output:

Lab 03: Console Input and Output 4


Programming Fundamentals (CS-116L) SSUET/QR/114

3. n1=int(input('"enter n1 value'))
n2=int(input('enter n2 value'))

C. Write Python programs for the following:

1. Write a program to print a student’s bio data having his/her Date of birth, Roll no,
Section, Percentage and grade of matriculation and Intermediate. All the fields
should be entered from the console at run time.

Code:

Output:

Lab 03: Console Input and Output 5


Programming Fundamentals (CS-116L) SSUET/QR/114

2. Write a program that asks the user what kind of food they would like. Print a
message about that food, such as “Let me see if I can find you a Chowmein”. Food
name must be in uppercase.
(hint: use upper( ) for food name)
Code:

Output:

3. Take the marks of 5 courses from the user and calculate the average and percentage,
display the result:
Eachcourse=50 marks
Total_marks= course1+course2+course3+course4+course5
average=Total_marks/5
percentage=(Total_marks x 100)/250

Code:

Output:
Lab 03: Console Input and Output 6
Programming Fundamentals (CS-116L) SSUET/QR/114

Lab 03: Console Input and Output 7

You might also like