0% found this document useful (0 votes)
8 views5 pages

Python Lab 02 Detailed

This lab manual covers the basics of Python programming, focusing on comments, input, output, and basic program structure. Key functions such as print() and input() are explained, along with type conversion and the use of escape sequences. The document also includes practice programs and exercises to reinforce learning.

Uploaded by

Asmara Minhas
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)
8 views5 pages

Python Lab 02 Detailed

This lab manual covers the basics of Python programming, focusing on comments, input, output, and basic program structure. Key functions such as print() and input() are explained, along with type conversion and the use of escape sequences. The document also includes practice programs and exercises to reinforce learning.

Uploaded by

Asmara Minhas
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

Programming Fundamentals Lab Manual (Python)

Lab 02 — Comments, Input, Output and Basic Programs

Introduction

Programming languages allow humans to communicate instructions to computers. In this lab we


study the most fundamental operations: displaying information, receiving input, documenting code,
and writing complete programs. Understanding these basics is essential before moving towards
decision making and loops.

Output using print()

The print() function sends data to the standard output device (monitor). It can display text, numbers,
variables and expressions.

print("Hello World")
print(25)
print(2+3)
x=10
print("Value of x:",x)

Escape Sequences

Escape Meaning
\n New line
\t Tab
\\ Backslash
\" Double quote
\' Single quote

print("Line1\nLine2")
print("Name:\tAli")

Input using input()

The input() function pauses the program and waits for the user to type a value. The entered value is
always stored as string, therefore conversion is required for numeric calculations.

name=input("Enter name: ")


age=int(input("Enter age: "))
cgpa=float(input("Enter CGPA: "))
print(name,age,cgpa)

Type Conversion

Python is strongly typed. Mathematical operations require compatible data types.

a="10"
b=5
print(int(a)+b)
Comments

Comments are ignored by the interpreter and help humans understand the code.

# This is a single line comment

"""
This program demonstrates
multi line documentation
"""

Complete Program Example

name=input("Enter name: ")


roll=input("Enter roll number: ")
course=input("Enter course: ")
print("\nStudent Details")
print("Name:",name)
print("Roll:",roll)
print("Course:",course)
Practice Programs

length=float(input("Length: "))
width=float(input("Width: "))
print("Area=",length*width)

c=float(input("Celsius: "))
print("Fahrenheit=",(c*9/5)+32)

p=float(input("Principal: "))
r=float(input("Rate: "))
t=float(input("Time: "))
print("SI=",(p*r*t)/100)
Exercises

Display name, print pattern, average of numbers, unit conversion, percentage calculation.

Viva Questions

Difference between input and print, type casting, escape sequences, comments, runtime errors.

You might also like