0% found this document useful (0 votes)
12 views15 pages

Python - File

File python btech

Uploaded by

jhaprince212003
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)
12 views15 pages

Python - File

File python btech

Uploaded by

jhaprince212003
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/ 15

HMR INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Hamidpur, Delhi-110036
(Affiliated to Guru Gobind Singh Indraprastha University, New Delhi)

Submitted in partial fulfilment of the requirements

For the award of degree of

Bachelor of Technology
In
Computer Science and Engineering
2021-2025
Programming in Python
Lab File
Code: CIE-332P

SUBMITTED TO: SUBMITTED BY:


Ms. PREETI VASU GUPTA
Assistant Professor 35513302721
CSE Department CSE 6-C

INDEX
S.NO. AIM OF EXPERIMENT DATE REMARKS SIGN
Basic data types and operators : Create a
program that prompts the user for their
1. name and age and prints a personalized
message.

Conditional statements : Create a program


that prompts the user for their age and tells
2. them if they can vote in the next election.

Loops : Create a program that calculates


3. the factorial of a number entered by the
user using a loop.

Lists and arrays : Create a program that


4. prompts the user for a list of numbers and
then sorts them in ascending order.

Strings and string manipulation : Create a


program that prompts the user for a string
5. and then prints out the string reversed.

Regular expressions : Create a program


that uses regular expressions to find all
6. instances of a specific pattern in a text file.

GUI programming : Create a program that


uses a graphical user interface (GUI) to
7. allow the user to perform simple
calculations.

Web scraping : Create a program that uses


a web scraping library to extract data from
8. a website and then stores it in a database.

Data visualization : Create a program that


reads data from a file and then creates a
9. visualization of that data using a data
visualization library.

Networking : Create a program that uses a


10. networking library to communicate with a
server and retrieve data from it.
EXPERIMENT - 1

AIM - Basic data types and operators : Create a program that prompts the user for their name and age and
prints a personalized message.

SOFTWARE USED - Visual Studio.

THEORY - # Data Types : It refers to the built-in types of data that a programming language inherently
understands. These typically include :

● Integer : Represents whole numbers, e.g., -1, 0, 1, 2.


● Float : Represents real numbers, can include a fractional part, e.g., 1.23, 3.14.
● Boolean : Represents True or False.
● String : Represents a sequence of characters, e.g., a, A.

These are fundamental to performing operations in the language and building more complex data
structures.

# Operators : They are symbols that represent specific actions to be performed on operands. These
typically include :

● Arithmetic Operators : Perform mathematical operations like addition (+), subtraction (-),
multiplication (*), and division (/).
● Comparison Operators : Compare two values and return True or False. Examples include equal to
(==), not equal to (!=), less than (<), and greater than (>).
● Logical Operators : Used to combine conditional statements. Includes and, or, and not.

CODE :

name = input("Please enter your name please : ")

age = int(input("\nPlease enter your age please : "))

print("\nMessage for you!")

print(f"\nHello, {name}! You are {age} years old.")

if age < 18:


print("\nRemember, the future belongs to those who believe in the beauty of their dreams. Keep
dreaming!\n")

elif age < 30:


print("\nYour 20s are your 'selfish' years. It's a decade to immerse yourself in every single thing
possible. Be adventurous with your time, all aspects of you and never stop learning.\n")
elif age < 50:
print("\nDon't worry about getting older. With age comes wisdom and confidence, and these traits
are beautiful!\n")

else:
print("\nAge is just a number. It's totally irrelevant unless, of course, you happen to be a tree. The
older the tree, the deeper the roots, and the brighter the blooms.\n")
OUTPUT :
EXPERIMENT - 2

AIM - Conditional statements : Create a program that prompts the user for their age and tells them if they can
vote in the next election.

SOFTWARE USED - Visual Studio.

THEORY - # Conditional Statements : This is a feature used to perform different computations or actions
depending on whether a specified boolean condition evaluates to true or false. Common types of conditional
statements include if, if-else, and switch statements.

CODE :
`
age = int(input("Please enter your age please : "))

if age >= 18:


print("\nYou are eligible to vote in the next election.\n")
else:
print("\nYou are not eligible to vote in the next election.\n")
OUTPUT :
EXPERIMENT - 3

AIM - Loops : Create a program that calculates the factorial of a number entered by the user using a loop.

SOFTWARE USED - Visual Studio.

THEORY -
# Loops : They are control flow structures that enable the repeated execution of a set of instructions or code
block as long as a specified condition is met. They are fundamental to the concept of iteration in
programming, enhancing code efficiency, readability, and promoting the reuse of code logic.

# Factorial : It’s a function that multiplies a given number, `n`, by every number less than `n` down to 1. This
operation is symbolized by an exclamation point (`!`). For example, the factorial of 4 (denoted as 4!) is
`4*3*2*1` which equals 24. Factorials are particularly useful in scenarios involving permutations and
combinations, as they can help determine the total number of possible outcomes.

CODE :

number = int(input("Please enter a number : "))

factorial = 1

for i in range(1, number + 1):


factorial *= i

print(f"\nThe factorial of {number} is {factorial}.\n")


OUTPUT :
EXPERIMENT - 4

AIM - Lists and arrays : Create a program that prompts the user for a list of numbers and then sorts them in
ascending order.

SOFTWARE USED - Visual Studio.

THEORY -
# Lists : It’s a data structure that represents a finite number of ordered values, where the same value may
occur more than once. It can store multiple data together in a single variable. The elements in a list can be of
different data types.

# Array : It’s a data structure consisting of a collection of elements (values or variables), each identified by at
least one array index or key. It is a collection of items of the same data type stored at contiguous memory
locations. The size of an array is set when created.

CODE :
import array

numbers = input("\nPlease enter numbers separated by a space : ")

numbers_list = list(map(int, numbers.split()))

numbers_array = array.array('i', numbers_list)

numbers_array = array.array('i', sorted(numbers_array))

sorted_list = list(numbers_array)

print(f"\nThe sorted list is : {sorted_list}\n")


OUTPUT :
EXPERIMENT - 5

AIM - Strings and string manipulation : Create a program that prompts the user for a string and then prints
out the string reversed.

SOFTWARE USED - Visual Studio.

THEORY -

# String : A string in programming is a sequence of characters, such as letters, numbers, and symbols. They
are widely used for storing and manipulating textual data in various programming languages. Strings can be
thought of as an array of characters.

# String Manipulation : String manipulation in programming refers to the process of modifying a string or
creating a new string by making changes to existing strings. This can involve a variety of operations such as
slicing, concatenation, repeating, interpolation, formatting, etc. These operations can be performed using
built-in string functions in many programming languages.

CODE :

text = input("Please enter a string : ")

reversed_text = text[::-1]

print(f"The reversed string is: {reversed_text}")


OUTPUT :
EXPERIMENT – 6

AIM - Regular expressions : Create a program that uses regular expressions to find all instances of a specific
pattern in a text file.

SOFTWARE USED - Visual Studio.

THEORY -

# Regular Expressions : It's a sequence of characters that forms a search pattern. This pattern is mainly used
for pattern matching with strings, or string matching, i.e., "find" or "find and replace" operations. Regular
expressions are used in every programming language like C++, Java, and Python. They are a generalized way
to match patterns with sequences of characters. The exact syntax and behavior of regular expressions can vary
between programming languages.

CODE :

import re

def find_pattern_in_text(text, pattern):


matches = re.findall(pattern, text)
return matches

file_name = input("\nEnter your file name please : ")


pattern = input("\nEnter the regular expression pattern you want to search for please : ")

with open(file_name, 'r') as file:


text = file.read()

matches = find_pattern_in_text(text, pattern)


for match in matches:
print(match)
TEXT FILE :

OUTPUT :

You might also like