Python - File
Python - File
Hamidpur, Delhi-110036
(Affiliated to Guru Gobind Singh Indraprastha University, New Delhi)
Bachelor of Technology
In
Computer Science and Engineering
2021-2025
Programming in Python
Lab File
Code: CIE-332P
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.
AIM - Basic data types and operators : Create a program that prompts the user for their name and age and
prints a personalized message.
THEORY - # Data Types : It refers to the built-in types of data that a programming language inherently
understands. These typically include :
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 :
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.
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 : "))
AIM - Loops : Create a program that calculates the factorial of a number entered by the user using a loop.
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 :
factorial = 1
AIM - Lists and arrays : Create a program that prompts the user for a list of numbers and then sorts them in
ascending order.
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
sorted_list = list(numbers_array)
AIM - Strings and string manipulation : Create a program that prompts the user for a string and then prints
out the string reversed.
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 :
reversed_text = text[::-1]
AIM - Regular expressions : Create a program that uses regular expressions to find all instances of a specific
pattern in a text file.
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
OUTPUT :