0% found this document useful (0 votes)
14 views8 pages

Lab Sheet 03

The document outlines objectives and exercises for a Python lab on operator precedence, type conversion, input/output, and escape sequences. Students will learn about implicit and explicit type conversion, taking input and output in Python, and using escape sequences. Exercises provide examples of code for these concepts and tasks for students to complete.

Uploaded by

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

Lab Sheet 03

The document outlines objectives and exercises for a Python lab on operator precedence, type conversion, input/output, and escape sequences. Students will learn about implicit and explicit type conversion, taking input and output in Python, and using escape sequences. Exercises provide examples of code for these concepts and tasks for students to complete.

Uploaded by

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

Lab Manual

Introduction to python
Lab-Sheet-03

Student Name

Reg. No

Department Computing & Technology

Batch/ Semester

Assigned Date

Due Date

Instructor
Mujtaba Awan
[email protected]
Week 3
Lab-sheet 3
Introduction to python Lab 3

Lab Objectives:

At the end of this lab students will know about.

 Operator Precedence and Associativity


 Type Conversion
 Implicit Type Conversion
 Explicit Type Conversion
 Input and Output
 Output Statements
 Escape Sequence

Operator Precedence and Associativity:

The computer scans an expression which contains the operators from left to right and performs only one
operation at a time. The expression will be scanned many times to produce the result. The order in which
various operations are performed is known as hierarchy of operations or operator precedence. Some of the
operators of the same level of precedence are evaluated from left to right or right to left. This is referred to
associativity.
Type Conversion
Converting one data type into another data type is called Type Conversion
Implicit Type Conversion.
In the Implicit type conversion, python automatically converts one data type into another data type.
Explicit Type Conversion.
In the Cast/Explicit Type Conversion, Programmer converts one data type into another data type.
Input and Output.
Input - The data given to the computer is called input.
Output – The results returned by the computer are called output.

LAB EXERCISES
The best way to teach programming is by example, and the only way to learn programming is by doing. So,
please do it yourself and don’t copy paste from others.
How to write Implicit Type Conversion code in python:

Example: 1
a=5
b=2
value = a/b
print(value)
print(type(value))

x = 10
y = 5.5
total = x + y
print(total)
print(type(total))

j = "Hello"
k = " python "
p=j+k
print(p)
print(type(p))

q = 20
u = '10'
r=q+u
print(r)

m = 20
n = python
t=m+n
print(t)

How to write Explicit Type Conversion code in python:

Example:
a=5
b=2
value = a/b
print(type(value))
int_value = int(value)
print(int_value)
print(type(int_value))
# String to Integer
q = 20
u = '10'
print(type(u))
r = q + int(u)
print(r)

# Float to Integer
n1 = 10.36
vn1 = int(n1)
print(vn1)
print(type(vn1))

# Integer to Float
n2 = 10
vn2 = float(n2)
print(vn2)
print(type(vn2))

# Integer to Complex
n3 = 10
vn3 = complex(n3)
print(vn3)
print(type(vn3))

# Integer to String
n4 = 10
vn4 = str(n4)
print(vn4)
print(type(vn4))

# String to Tuple
n5 = "python"
vn5 = tuple(n5)
print(vn5)
print(type(vn5))

# Tuple to List
n5 = ("ali", "khan", "danish", "usman")
vn5 = list(n5)
print(vn5)
print(type(vn5))
Example:

# print string with double quotes


print("Welcome to Python")

# print blank line


print()

# print string with Single quotes


print('Welcome to Python)

# print each word in same line becoz inside one print function
print("Like", "Share", "Subscribe")

# print integer
print(10)

# prints in separate line


print("Welcome")
print("to")
print("Python ")

# print list
data = [10, 20, -50, 21.3, ' Python ']
print(data)

# print with separator


print("Like", "Share", "Subscribe", sep="***")

# print with end 'space'


print("Welcome", end="")
print("to", end="")
print("Python ")

# print with end 'tab'


print("We", end="\t")
print("to", end="\t")
print("Ge")

# print variable value


a = 10
print(a)
# print variable value with separator
x = 20
y = 30
print(x, y)
print(x, y, sep=',')

# print with integer and string


m = 40
print("Value: ", m)

# print with integer and string


name = "ali"
age = 62
print("My Name is ", name, "and My age is ", age)

How to take output code in python:

# print string with double quotes


print("Welcome to python")

# print blank line


print()

# print string with Single quotes


print('Welcome to python)

# print each word in same line becoz inside one print function
print("Like", "Share", "Subscribe")

# print integer
print(10)

# prints in separate line


print("Welcome")
print("to")
print("python ")

# print list
data = [10, 20, -50, 21.3, ' python']
print(data)

# print with separator


print("Like", "Share", "Subscribe", sep="***")

# print with end 'space'


print("Welcome", end="")
print("to", end="")
print("python")

# print with end 'tab'


print("We", end="\t")
print("to", end="\t")
print("Ge")

# print variable value


a = 10
print(a)

# print variable value with separator


x = 20
y = 30
print(x, y)
print(x, y, sep=',')

# print with integer and string


m = 40
print("Value: ", m)

# print with integer and string


name = "ali"
age = 62
print("My Name is ", name, "and My age is ", age)

Tasks
How to take input code in python:
How to use escape sequence in python:

You might also like