Python LAB 03
Python LAB 03
LAB # 03
OBJECTIVE
Taking input from user and controlling output position.
THEORY
Syntax : input(prompt)
The message can be a string, or any other object, the object will be converted into a
string before written to the screen.
Example:
name=input('Please enter your name: "') print("Hello,
" , name , "!")
Output:
>>> %Run task1.py Please enter your
name:ABC Hello, ABC!
>>>
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)
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())
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!")
3. age = 23
message = "Happy " + age + "rd Birthday!"
print(message)
Correct Code:
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
1. a=5
print("a =", a, sep='0', end=',')
Output:
3. n1=int(input('"enter n1 value'))
n2=int(input('enter n2 value'))
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:
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