Assignment
Assignment
Fundamentals of Programming
Contents
Fundamentals:..................................................................................................................................2
Why Python?...............................................................................................................................2
Description:..................................................................................................................................3
Some of the concepts we have used:...........................................................................................3
Operations:...................................................................................................................................3
Logical Operators:.......................................................................................................................4
Boolean Expressions:...................................................................................................................4
Nested IF Else:.............................................................................................................................4
While Loop:.................................................................................................................................4
For Loops:....................................................................................................................................4
Methods:......................................................................................................................................4
String Built-in Methods:..............................................................................................................5
Summary:.....................................................................................................................................6
1
Fundamentals:
Why Python?
Python's versatility and simplicity have made it a beloved programming language across a wide
range of applications. Its clean and readable syntax is particularly appealing to beginners, while
its extensive libraries and frameworks cater to professionals in fields as diverse as web
development, data analysis, artificial intelligence, and scientific research. Python's cross-
platform compatibility allows developers to create applications that run seamlessly on different
operating systems. Additionally, its strong community support means that developers have
access to a wealth of resources, tutorials, and third-party packages, making it an ideal choice for
rapid prototyping and development. Whether we’re building web applications, automating tasks,
analyzing data, or developing cutting-edge machine learning models, Python's adaptability and
user-friendly nature make it an invaluable tool in the world of programming.
2
Description:
We have learnt different fundamentals of python in the week 1, 2, 3, 4, 5, 6 and 7. In the program
we have used different terms and our concepts in the form of python programming.
Some of the concepts we have used:
Lists
Methods
For Loop
String Methods
If statement and nested statements
Logical Operators and Boolean Expressions
Here in the program we were having a dataset, which is generated from Chatgpt we have saved it
into the List of Python, we have applied various different techniques to check the dataset and its
validity.
This Python program operates on a given dataset, which contains information about individuals.
It offers various functionalities through a menu-driven interface. Here's a breakdown of the
program's functionality:
Dataset Initialization: The program begins with the initialization of a dataset containing
information about individuals, including their name, age, gender, city, and occupation.
Menu-Driven Interface: The program presents a menu to the user with several options,
ranging from checking the dataset's length to applying string methods and logical
conditions. The user is prompted to enter a numeric choice (1-9) to select an operation or
0 to exit the program.
Operations:
Check the length of the dataset (the number of records).
Append a new record to the dataset.
Run a for loop to display the dataset's contents.
Append a new record using a user-defined method.
Convert a string into uppercase and display the result.
Calculate and display the length of a string.
Convert a string into lowercase and display the result.
Remove a row (record) from the dataset based on a specified name.
Apply a logical condition to filter records based on age and display the results.
Error Handling: The program handles invalid input by providing a message to the user
when input is not a valid choice (e.g., non-numeric, out of range).
Program Termination: The program can be terminated by entering '0' in response to the
menu prompt.
3
Logical Operators:
Logical operators in Python include and, or, and not.
They are used to combine or manipulate Boolean values (True or False).
And returns true if both conditions are true.
Or returns true if at least one condition is true.
Not negates the Boolean value.
Boolean Expressions:
Boolean expressions are statements that evaluate to either True or False. They are used in
conditional statements (if, elif, else) and loops for decision-making.
Nested IF Else:
Nested if-else statements allow you to have conditional statements inside other conditional
statements. They are used when you need to make multiple decisions based on multiple
conditions.
While Loop:
A while loop is a control structure that repeatedly executes a block of code as long as a given
condition is true. It's used for tasks that need to be repeated until a specific condition is met.
For Loops:
For loops are used to iterate over a sequence (e.g., lists, strings, tuples, and dictionaries). They
execute a block of code for each item in the sequence. For loops are often used when the number
of iterations is known.
Methods:
Methods (or functions) are blocks of reusable code that perform specific tasks. They help in
organizing and modularizing code. Python has built-in methods, and you can create your own
custom functions. In programming, methods and functions are crucial because they make it
possible to write modular, and well-organized code. The independent chunks of code known as
functions carry out certain tasks and have the ability to receive input arguments and return
outcomes. It encourage code reuse and aid in the division of difficult issues into more
manageable parts. Since each kind of method has a specific function, developers may create
classes that are precise and flexible while still following the rules of object-oriented
programming.
4
String Built-in Methods:
Python offers a wide range of built-in methods for string manipulation.
Examples include upper() (converts a string to uppercase), lower() (converts to lowercase), and
len() (returns the length). These string methods serve as essential tools for text manipulation,
cleansing, and analysis in Python. They simplify common text processing tasks, making it easier
for developers to work with textual data in their Python programs.
In Python, these programming fundamentals are essential for controlling program flow, making
decisions, and working with data. They are foundational concepts that enable us to create more
complex and functional Python programs.
5
Pseudo code:
Step 1 - Start an infinite loop using a 'while True' statement.
Step 2 - Display a menu to the user with the following options:
- "1- To Check the Length of Dataset"
- "2- Append Methods"
- "3- To Run For Loop"
- "4- To Append By User Defined Methods or Functions"
- "5- Convert String into Uppercase"
- "6- Check String Length"
- "7- Convert String into Lowercase"
- "8- Remove Field from Dataset"
- "9- Apply Logical Condition on Dataset"
- "0- Program Exit"
- "*****************Press Any Digit Only*****************"
Step 3 - Prompt the user to enter a digit as input and store it in the variable 'user_input'.
Step 4 - Check if 'user_input' is a valid digit:
a. If it's a valid digit, convert it to an integer and store it in 'number'.
b. Check if 'number' is equal to 0:
- Display "Exiting the program."
- Break out of the loop to exit the program.
Step 5 - If 'number' is not 0, check the value of 'number' using a series of 'elif' statements:
- If 'number' is 1:
- Display "Number of records in the dataset:" followed by the length of the 'dataset'.
- If 'number' is 2:
- Display "You Selected Append Methods."
- Display "Size of Dataset before Appending is:" followed by the length of the
'dataset'.
- Create a new record ('new_record') with sample data.
- Append 'new_record' to the 'dataset'.
- Display each record in the 'dataset'.
6
- Display "After Appending a single record, the length of dataset is now:" followed
by the updated length of the 'dataset'.
- If 'number' is 3:
- Display "You Selected For Loop for Running."
- Iterate through the 'dataset' and display each record's fields.
- If 'number' is 4:
- Display "You Selected Method Condition."
- Define a function 'add_record' that appends a new record to the 'dataset'.
- Create a new record ('new_record') with sample data.
- Call the 'add_record' function to append 'new_record' to the 'dataset'.
- Display the updated 'dataset'.
- If 'number' is 5:
- Extract the first name from the first record in the 'dataset'.
- Display "You Selected String Upper Method, We will take the first value of
dataset, which is:" followed by the first name.
- Convert the first name to uppercase using the '.upper()' method and display the
result.
- If 'number' is 6:
- Extract the first name from the first record in the 'dataset'.
- Display "You Selected String Length Method, We will take the first value of
dataset, which is:" followed by the first name.
- Calculate and display the length of the first name using the 'len()' function.
- If 'number' is 7:
- Extract the first name from the first record in the 'dataset'.
- Display "You Selected String Lower Method, We will take the first value of
dataset, which is:" followed by the first name.
- Convert the first name to lowercase using the '.lower()' method and display the
result.
- If 'number' is 8:
- Display "You Selected List Removal Number."
- Define a function 'remove_row_by_name' that removes a record from the 'dataset'
based on the name provided.
- Call the 'remove_row_by_name' function to remove a record with the name "Bob"
from the 'dataset'.
- Display the updated 'dataset'.
- Display "Now Updated Length of Dataset is:" followed by the updated length of
the 'dataset'.
7
- If 'number' is 9:
- Display "You Selected Logical Condition on Dataset."
- Create a list 'young_or_old' containing records from the 'dataset' where the age is
less than 25 or greater than 40.
- Display the records in the 'young_or_old' list.
- If 'number' is greater than 9 or less than 0, display "Number is Not Valid, Please
try Again."
Step 6 - If 'user_input' is not a valid digit, display "Invalid input. Please enter a valid number (1-
9) or 0 to exit."
Step 7 - End of the loop. The program will continue to prompt the user until they enter 0 to exit.
8
Flowchart:
9
Summary:
This program demonstrates a variety of Python concepts, including lists, loops, functions,
conditional statements, and string manipulation, in the context of managing and manipulating a
dataset. Users can interact with the dataset and perform various operations based on their needs.
10