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

Python Lab 123-2

The document outlines a Python Programming course at Lakshmi Narain College of Technology, detailing various experiments and objectives for students. Each experiment focuses on different aspects of Python, including basic syntax, data types, input/output operations, and type conversion. The document includes program examples and exercises to reinforce learning outcomes.

Uploaded by

nevedchouksey633
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)
3 views20 pages

Python Lab 123-2

The document outlines a Python Programming course at Lakshmi Narain College of Technology, detailing various experiments and objectives for students. Each experiment focuses on different aspects of Python, including basic syntax, data types, input/output operations, and type conversion. The document includes program examples and exercises to reinforce learning outcomes.

Uploaded by

nevedchouksey633
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

Lakshmi Narain College of Technology, Bhopal (M.

P)

Department of Computer Science and Engineering

Name of the faculty: Yogesh Sahu Session (2023-24)

Subject: Python Programming Subject Code

Semester: IV Year: II

Index

List Of Experiments Date of Remark


[Link].
Experiment

1 Introduction to Python & Basic


Syntax
2 Write a program to display
student details using
input/output.
3 Study of basic constructs in
python programming followed
with
exercise.
4 Study of different data types in
python, followed with exercise.
5 Study of different sequence data
types in python followed with
exercise.
6 Convert data types (int, float, string)
using user input.
7

9
10
Experiment 01

Aim: - Introduction to Python & Basic Syntax.

Objective:
To understand basic Python syntax
To learn variables, constants, and data types
To perform input and output operations
To write simple Python programs

Theory:

Python Basic: Python is a high-level, interpreted programming language


known for its simple syntax and readability.

1. Literal Constants: Literal constants are fixed values:

Numeric → 10, 3.14


String → "Hello"
Boolean → True, False

2. Variables

Variables store data values.

x = 10
name = "Yogesh"

3. Data Types

Data Type Example


Integer 10
Float 3.14
String "Hello"
Boolean True
4. Input and Output

name = input("Enter name: ")


print("Hello", name)

5. Comments

# This is a single-line comment

6. Indentation

Python uses indentation instead of braces {}.

if True:
print("Hello")

Programs:

Program 1: Basic Input and Output

# Program to display user information

name = input("Enter your name: ")


age = int(input("Enter your age: "))

print("Name:", name)
print("Age:", age)
Program 2: Demonstration of Data Types
# Program to show different data types

a = 10
b = 3.5
c = "Python"
d = True

print("Integer:", a)
print("Float:", b)
print("String:", c)
print("Boolean:", d)

Program 3: Simple Arithmetic Calculation


# Program to perform arithmetic operations

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))

print("Addition:", num1 + num2)


print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)

Program 4: Area of a Circle

# Program to calculate area of a circle

radius = float(input("Enter radius: "))


area = 3.14 * radius * radius

print("Area of circle:", area)


Program 5: Swap Two Numbers
# Program to swap two numbers

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))

a, b = b, a

print("After swapping:")
print("a =", a)
print("b =", b)
Experiment 02

Aim: - Write a program to display student details using


input/output.

Objective:
To learn how to take input from the user
To understand output display in Python
To develop a simple Python program using input/output functions

Theory:

Python provides simple functions for input and output operations:

Input Function

input() is used to take data from the user

It always takes input as string

Example:
name = input("Enter your name: ")

Output Function:

print() is used to display output

Type Conversion:

Convert input into required type using:

int() → integer
float() → decimal number
Program Code:

# Program to display student details

# Taking input from user


name = input("Enter Student Name: ")
roll_no = int(input("Enter Roll Number: "))
branch = input("Enter Branch: ")
year = input("Enter Year: ")

# Displaying output
print("\n--- Student Details ---")
print("Name:", name)
print("Roll Number:", roll_no)
print("Branch:", branch)
print("Year:", year)

Sample Output:

Enter Student Name: Yogesh


Enter Roll Number: 101
Enter Branch: AIML
Enter Year: 2nd Year

--- Student Details ---


Name: Yogesh
Roll Number: 101
Branch: AIML
Year: 2nd Year
Experiment 03

Aim: - Study of basic constructs in python programming


followed with

exercise.

Objective:
To understand basic constructs of Python programming
To learn variables, input/output, comments, and indentation
To write simple Python programs using basic constructs

Theory:

1. Variables

Variables are used to store data values.

x = 10
name = "Python"

2. Input and Output

 input() → takes input from user


 print() → displays output

3. Keywords

 Reserved words like:


if, else, for, while, def, etc.

Program 1: Basic Input and Output

# Program to display user name

name = input("Enter your name: ")


print("Welcome", name)
Program 2: Using Variables and Expressions

# Program to perform addition

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))

sum = a + b
print("Sum is:", sum)

Program 3: Demonstration of Comments

# This program shows use of comments

# Taking input
num = int(input("Enter a number: "))

# Display output
print("You entered:", num)

Program 4: Indentation Example

# Program to check number is positive

num = int(input("Enter a number: "))

if num > 0:
print("Positive Number")

Program 5: Multiple Variables

# Program to store multiple values

name = "Yogesh"
age = 25
city = "Bhopal"

print(name, age, city)

Result
The basic constructs of Python programming were studied and implemented
successfully.
Experiment 04

Aim: - Study of different data types in python, followed


with exercise.

Objective
 To understand different data types in Python
 To learn how to use and identify data types
 To perform operations using different data types

1. What is a Data Type?

A data type defines the type of data a variable can store.

2. Basic Data Types in Python

Data
Description Example
Type
Integer
int 10
numbers
Decimal
float 3.14
numbers
str String (text) "Python"
True /
bool Boolean
False

3. Checking Data Type

We use type() function.

x = 10
print(type(x))
4. Type Conversion

 int() → convert to integer


 float() → convert to float
 str() → convert to string

Programs:
Program 1: Demonstration of Basic Data Types

# Program to show different data types

a = 10
b = 3.5
c = "Python"
d = True

print("Integer:", a)
print("Float:", b)
print("String:", c)
print("Boolean:", d)

Program 2: Check Data Type

# Program to check data type

x = input("Enter something: ")

print("You entered:", x)
print("Data type:", type(x))

Program 3: Type Conversion

# Program for type conversion

num = input("Enter a number: ")

print("Integer:", int(num))
print("Float:", float(num))
print("String:", str(num))

Program 4: Arithmetic Operations

# Program using int and float

a = int(input("Enter integer: "))


b = float(input("Enter float: "))

print("Addition:", a + b)
print("Multiplication:", a * b)

Program 5: Boolean Example

# Program using boolean

num = int(input("Enter a number: "))

print("Is number positive?", num > 0)

Result
Different data types in Python were studied and implemented successfully.

Conclusion
In this experiment, we learned about different data types in Python and how to
use them effectively. Data types are fundamental for handling data in any
program.
Experiment 05

Aim: - Study of Different Sequence Data Types in Python


followed with Exercise

Objective
 To understand sequence data types in Python
 To learn operations on list, tuple, and string
 To perform programs using sequence data types

Theory

1. What are Sequence Data Types?

Sequence data types are used to store multiple values in an ordered manner.

Elements can be accessed using indexing and slicing.

2. Types of Sequence Data Types

(A) List

 Ordered, mutable (changeable)


 Allows duplicate values

list1 = [1, 2, 3, 4]
B) Tuple

 Ordered, immutable (cannot be changed)

tuple1 = (10, 20, 30)

(C) String

 Sequence of characters

str1 = "Python"

3. Common Operations
 Indexing → list[0]
 Slicing → list[1:3]
 Length → len()
 Concatenation → +

Programs
# Program to demonstrate list operations

list1 = [10, 20, 30, 40]

print("Original List:", list1)

[Link](50)
print("After Append:", list1)

[Link](20)
print("After Remove:", list1)

Program 2: Tuple Operations

# Program to demonstrate tuple

tuple1 = (1, 2, 3, 4)

print("Tuple:", tuple1)
print("First Element:", tuple1[0])
print("Length:", len(tuple1))

Program 3: String Operations

# Program to demonstrate string operations

str1 = "Python Programming"


print("String:", str1)
print("First Character:", str1[0])
print("Slice:", str1[0:6])

Program 4: Indexing and Slicing

# Program for indexing and slicing

list1 = [1, 2, 3, 4, 5]

print("Index 2:", list1[2])


print("Slice 1-3:", list1[1:4])

Program 5: Concatenation

# Program to concatenate sequences

list1 = [1, 2]
list2 = [3, 4]

print("Concatenated List:", list1 + list2)

str1 = "Hello "


str2 = "Python"

print("Concatenated String:", str1 + str2)

Result

Sequence data types (list, tuple, string) were studied and implemented
successfully.
Experiment 06

Aim: - Convert Data Types (int, float, string) using User


Input

Objective
 To understand type conversion in Python
 To learn conversion between integer, float, and string
 To perform type casting using user input

Theory

1. What is Type Conversion?

Type conversion means changing one data type into another data type.

Python provides built-in functions for conversion:

 int() → converts into integer


 float() → converts into float
 str() → converts into string

2. Why Type Conversion is Required?


The input() function takes data as string by default.

Example:

num = input("Enter number: ")

Even if user enters 10, Python stores it as "10" (string).

Therefore, conversion is needed for mathematical operations.

3. Types of Type Conversion

Function Description Example


Convert to
int() int("10")
integer
Convert to
float() float("3.5")
float
Convert to
str() str(100)
string

Programs
Program 1: Convert String to Integer

# Program to convert string input into integer

num = input("Enter a number: ")

print("Before Conversion:", type(num))

num = int(num)

print("After Conversion:", type(num))


print("Value:", num)

Program 2: Convert Integer to Float

# Program to convert integer into float

num = int(input("Enter integer value: "))

f = float(num)

print("Float Value:", f)
print("Data Type:", type(f))
Program 3: Convert Float to Integer

# Program to convert float into integer

num = float(input("Enter float value: "))

i = int(num)

print("Integer Value:", i)
print("Data Type:", type(i))

Program 4: Convert Number into String

# Program to convert integer into string

num = int(input("Enter a number: "))

s = str(num)

print("String Value:", s)
print("Data Type:", type(s))

Program 5: Mixed Type Conversion

# Program for multiple conversions

num = input("Enter a number: ")

print("Original Type:", type(num))

i = int(num)
f = float(num)
s = str(num)

print("Integer:", i)
print("Float:", f)
print("String:", s)

Result

Type conversion between integer, float, and string was performed successfully using
user input.

Exercises
Exercise 1:

Take age as input and convert it into integer.

Exercise 2:

Convert decimal number entered by user into integer.

Exercise 3:

Convert integer marks into string and display.

Exercise 4:

Take two numbers as input and display their sum after conversion.

Exercise 5:

Check data type before and after conversion.

You might also like