PYTHON
UNIT 1
SYLLABUS
Python Introduction, History & Installing of Python, Python basics, Python Objects, Standard
Types, Other Built-in Types, Internal Types, Standard Type Operators, Standard Type Built-in
Functions, Categorizing the Standard Types, Unsupported Types Numbers - Introduction to
Numbers, Integers, Floating Point Real Numbers, Complex Numbers, Operators, Built-in
Functions.
10 DESCRIPTIVE QUESTIONS
S.No Question BL CO
1 Explain the historical development and key features of Python as a 2 CO-1
programming language.
2 Describe the process of installing Python and setting up a development 3 CO-1
environment.
3 Illustrate the basic syntax and structure of a Python program with an 3 CO-1
example.
4 Compare and contrast Python objects with standard data types used in other 2 CO-1
programming languages.
5 Explain the significance of built-in functions in Python with examples. 2 CO-1
6 Classify Python’s standard types into categories and provide examples for 2 CO-1
each category.
7 Evaluate the advantages and disadvantages of using complex numbers in 5 CO-1
Python for engineering calculations.
8 Design a Python program to calculate the bending moment and shear force 4 CO-1
of a beam.
9 Justify the use of Python over other programming languages for solving 3 CO-1
numerical methods in civil engineering.
10 Develop a Python script that categorizes materials (steel, concrete, etc.) 5 CO-1
based on input properties like strength and density.
20 objective questions along with answers
1. Who developed Python?
A. Dennis Ritchie | B. Guido van Rossum | C. James Gosling | D. Bjarne Stroustrup
Answer: B. Guido van Rossum
2. What type of language is Python?
A. Compiled | B. Interpreted | C. Assembly | D. None of the above
Answer: B. Interpreted
3. Which command is used to check the Python version?
A. python --v | B. python -version | C. python --version | D. python -v
Answer: C. python --version
4. What is the correct file extension for Python files?
A. .pyth | B. .pt | C. .py | D. .python
Answer: C. .py
5. Which data type is immutable in Python?
A. List | B. Dictionary | C. String | D. Set
Answer: C. String
6. How do you start a comment in Python?
A. // | B. # | C. /* | D. <!--
Answer: B. #
7. Which of the following is not a Python keyword?
A. pass | B. eval | C. assert | D. nonlocal
Answer: B. eval
8. What is the output of print(2 ** 3)?
A. 6 | B. 8 | C. 9 | D. 12
Answer: B. 8
9. What does len() function do in Python?
A. Finds the length of an object | B. Finds the data type | C. Converts to string | D.
Deletes an object
Answer: A. Finds the length of an object
10. Which method is used to add an item to a list?
A. add() | B. append() | C. insert() | D. extend()
Answer: B. append()
11. How is a function defined in Python?
A. function | B. def | C. fun | D. define
Answer: B. def
12. Which of the following is used for string formatting in Python?
A. % | B. $ | C. @ | D. #
Answer: A. %
13. Which of these is a valid variable name?
A. 1variable | B. @var | C. _variable | D. variable-name
Answer: C. _variable
14. Which module in Python is used for random number generation?
A. os | B. sys | C. random | D. math
Answer: C. random
15. What is the output of bool([])?
A. True | B. False | C. Error | D. None
Answer: B. False
16. Which operator is used for floor division in Python?
A. / | B. // | C. % | D. **
Answer: B. //
17. What is the correct way to open a file in write mode?
A. open('file.txt', 'w') | B. open('file.txt', 'r') | C. open('file.txt', 'a') | D. open('file.txt',
'rw')
Answer: A. open('file.txt', 'w')
18. What does the is operator do in Python?
A. Checks value equality | B. Checks object identity | C. Compares types | D. Assigns
values
Answer: B. Checks object identity
19. Which of the following is a mutable data type?
A. Tuple | B. String | C. List | D. Integer
Answer: C. List
20. What does pip stand for in Python?
A. Python Index Package | B. Preferred Installer Program | C. Python Installation
Package | D. Program Installation Package
Answer: B. Preferred Installer Program
ASSIGNMENT QUESTIONS
S.No Question BL CO
1 Research and write a short essay on the evolution of Python as a 2 CO-1
programming language. Include key milestones in its development.
2 Install Python on your system and configure an Integrated Development 3 CO-1
Environment (IDE) of your choice (e.g., PyCharm, VS Code, or Jupyter
Notebook). Document the installation process with screenshots.
3 Write a Python program to perform the following tasks: 6 CO-1
Calculate the area and perimeter of a rectangle.
Check whether a given number is prime or not.
Convert a string into uppercase and count the number of
vowels in it.
4 Explain the difference between mutable and immutable data types 4 CO-1
in Python with examples. Write Python code to demonstrate how
lists (mutable) and tuples (immutable) behave differently when
modified.
5 Develop a Python program that: 6 CO-1
Accepts a list of numbers as input.
Calculates the sum, average, and standard deviation of the
numbers.
Categorizes the numbers as positive, negative, or zero.
MOOCS course or NPTEL courses
1. Programming, Data Structures and Algorithms Using Python
https://onlinecourses.nptel.ac.in/noc24_cs45/preview
2. Python for Data Science
https://onlinecourses.nptel.ac.in/noc22_cs32/preview
3. The Joy of Computing using Python
https://onlinecourses.nptel.ac.in/noc24_cs57/preview
4. Programming in Python
https://onlinecourses.swayam2.ac.in/cec22_cs20/preview
5. NOC: Programming, Data Structures and Algorithms in Python
https://archive.nptel.ac.in/courses/106/106/106106145
REAL TIME APPLICATION
The image visually represent the basic flow of operations within a Python program and
demonstrate the relationship between the various Python types, objects, and operators. It will
also showcase how the user interacts with the calculator program. The diagram illustrates the
relationships between Python's basic concepts, including numbers, variables, operators, and
built-in functions in a Python program. It shows how different Python types like integers,
floats, and complex numbers interact with operators like +, -, *, and /. This example is part of
a real-time application, like the calculator we discussed earlier.
1. Introduction to Python for Engineering Applications
o Discuss Python's significance in solving real-world engineering problems.
Explain how Python can automate tasks, process data, and solve complex
equations in the civil engineering domain.
2. Data Analysis and Visualization in Civil Engineering Using Python
o Cover tools like Pandas, Matplotlib, and Seaborn for analyzing and visualizing
engineering data (e.g., construction data, material analysis, environmental
factors).
3. Python for Structural Analysis and Design
o Explain how Python can be used to solve structural engineering problems such
as beam bending, trusses, and reinforcement design. Show how to use Python
libraries like NumPy and SciPy for these calculations.
4. Automation in Civil Engineering Project Management with Python
o Discuss how Python can help automate project management tasks such as
scheduling, budgeting, and resource allocation in construction projects.
5. Introduction to Python's Standard and Built-in Types for Engineering
Calculations
o Present Python’s number types (integers, floating-point numbers, complex
numbers) and show how to apply them in engineering calculations, including
structural analysis and fluid dynamics.
Minor Project Ideas:
1. Automated Concrete Mix Design Using Python
o Develop a Python program that automatically calculates the mix ratio of
concrete based on various parameters such as strength, type of cement,
aggregates, and environmental conditions.
2. Traffic Flow Analysis Using Python
o Create a Python tool to analyze traffic data (e.g., vehicle counts, speeds,
congestion) and visualize the results to help in planning road networks and
improving urban traffic systems.
UNIT TEST
SET 1
Answer any two questions
All questions carry equal marks MAX MARKS: 10
S.No Question BL CO
1 Explain the history of Python and its evolution as a programming 2 CO-1
language. How does Python's simplicity benefit engineering students?
2 List and explain the standard types in Python with examples. How are 3 CO-1
they different from other built-in types?
3 Describe the process of installing Python on a computer. What are some 2 CO-1
common installation issues faced by beginners?
4 What are the different number types in Python? Provide examples for 2 CO-1
integers, floating-point numbers, and complex numbers.
UNIT TEST
SET 2
Answer any two questions
All questions carry equal marks MAX MARKS: 10
S.No Question BL CO
1 Discuss Python's role in civil engineering and explain how it can be used 2 CO-1
to solve real-world problems.
2 What are operators in Python? Describe arithmetic operators and their 3 CO-1
usage with examples.
3 What are built-in functions in Python? Explain some commonly used 2 CO-1
functions related to numbers, such as abs(), round(), and pow().
4 Describe the concept of unsupported types in Python. Provide examples 2 CO-1
of how unsupported types may affect code execution.
10 analytical/programming problems
1. Simple Arithmetic Operations with Python
Problem: Write a Python program to perform addition, subtraction, multiplication, and
division on two numbers provided by the user.
# Program to perform arithmetic operations
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)
Explanation: This program demonstrates basic arithmetic operations using Python's built-in
operators.
2. Check Even or Odd Number
Problem: Write a Python program to check whether a given number is even or odd.
# Program to check even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Explanation: This program uses the modulus operator to determine if the number is divisible
by 2 (even) or not (odd).
3. Complex Number Operations
Problem: Write a Python program to perform addition, subtraction, and multiplication of two
complex numbers.
# Program to perform operations on complex numbers
num1 = complex(input("Enter first complex number: "))
num2 = complex(input("Enter second complex number: "))
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
Explanation: Python’s complex type allows complex number operations directly, and basic
arithmetic operations can be applied directly to them.
4. Finding the Maximum of Three Numbers
Problem: Write a Python program to find the maximum of three numbers.
# Program to find the maximum of three numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
max_num = max(num1, num2, num3)
print(f"The maximum number is: {max_num}")
Explanation: This program uses Python’s built-in max() function to compare three numbers
and find the largest.
5. Sum of N Natural Numbers
Problem: Write a Python program to calculate the sum of the first N natural numbers.
# Program to find sum of first N natural numbers
N = int(input("Enter the value of N: "))
sum_n = N * (N + 1) // 2
print(f"The sum of the first {N} natural numbers is: {sum_n}")
Explanation: The formula for the sum of the first N natural numbers is used here to calculate
the result directly.
6. Python Program for Factorial Using Recursion
Problem: Write a Python program to find the factorial of a given number using recursion.
# Program to find factorial using recursion
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter a number: "))
print(f"The factorial of {num} is: {factorial(num)}")
Explanation: This program uses recursion to calculate the factorial, where the function calls
itself with a reduced value until it reaches 0.
7. Python Program to Convert Celsius to Fahrenheit
Problem: Write a Python program that converts Celsius to Fahrenheit.
# Program to convert Celsius to Fahrenheit
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius} Celsius is equal to {fahrenheit} Fahrenheit.")
Explanation: The program uses the formula to convert Celsius to Fahrenheit: F = (C × 9/5) +
32.
8. Finding the Area of a Circle
Problem: Write a Python program to calculate the area of a circle given the radius.
# Program to find the area of a circle
import math
radius = float(input("Enter the radius of the circle: "))
area = math.pi * radius ** 2
print(f"The area of the circle is: {area}")
Explanation: This program calculates the area of a circle using the formula Area = π *
radius^2.
9. Python Program to Find the Roots of a Quadratic Equation
Problem: Write a Python program to find the roots of a quadratic equation ax^2 + bx + c = 0.
# Program to find roots of quadratic equation
import cmath
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
# Calculate discriminant
d = cmath.sqrt(b**2 - 4*a*c)
# Find two roots
root1 = (-b + d) / (2*a)
root2 = (-b - d) / (2*a)
print(f"The roots are: {root1} and {root2}")
Explanation: The program uses the quadratic formula to calculate the roots. The cmath
library handles complex roots.
10. Python Program to Solve System of Linear Equations
Problem: Solve the system of linear equations using Python:
2x+3y=54x+y=62x + 3y = 5 4x + y = 62x+3y=54x+y=6
# Program to solve a system of linear equations
import numpy as np
# Coefficients of the equations
A = np.array([[2, 3], [4, 1]])
B = np.array([5, 6])
# Solve the system of equations
solution = np.linalg.solve(A, B)
print(f"The solution is x = {solution[0]} and y = {solution[1]}")
Explanation: This program uses NumPy's linalg. solve() method to solve the system of
linear equations by using matrix operations.
Solved tutorial questions
1. Python Program to Perform Basic Arithmetic Operations
Question: Write a Python program to perform the following operations on two numbers:
addition, subtraction, multiplication, and division.
Solution:
# Taking user input for two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Performing basic arithmetic operations
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2
# Printing the results
print(f"Addition: {addition}")
print(f"Subtraction: {subtraction}")
print(f"Multiplication: {multiplication}")
print(f"Division: {division}")
Explanation: This program takes two inputs from the user and performs all basic arithmetic
operations using Python’s built-in operators.
2. Python Program to Check If a Number Is Even or Odd
Question: Write a Python program to check if a given number is even or odd.
Solution:
# Taking user input for a number
num = int(input("Enter a number: "))
# Checking if the number is even or odd
if num % 2 == 0:
print("The number is Even.")
else:
print("The number is Odd.")
Explanation: This program checks if the number is divisible by 2 using the modulus operator
(%). If the remainder is 0, the number is even, otherwise, it's odd.
3. Python Program to Calculate the Factorial of a Number Using Recursion
Question: Write a Python program to calculate the factorial of a number using recursion.
Solution:
# Function to find factorial using recursion
def factorial(n):
if n == 0: # Base case
return 1
else:
return n * factorial(n - 1)
# Taking user input
num = int(input("Enter a number: "))
print(f"The factorial of {num} is: {factorial(num)}")
Explanation: This program uses recursion to calculate the factorial of a number. The
function calls itself with the number reduced by 1 until it reaches the base case (0), which
returns 1.
4. Python Program to Find the Sum of N Natural Numbers
Question: Write a Python program to calculate the sum of the first N natural numbers.
Solution:
# Taking user input
N = int(input("Enter the value of N: "))
# Formula to find the sum of first N natural numbers
sum_n = N * (N + 1) // 2 # Integer division
print(f"The sum of first {N} natural numbers is: {sum_n}")
Explanation: The program uses the formula for the sum of the first N natural numbers:
N(N+1)2\frac{N(N + 1)}{2}2N(N+1).
5. Python Program to Convert Celsius to Fahrenheit
Question: Write a Python program to convert temperature from Celsius to Fahrenheit.
Solution:
# Taking user input for Celsius temperature
celsius = float(input("Enter temperature in Celsius: "))
# Formula to convert Celsius to Fahrenheit
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius} Celsius is equal to {fahrenheit} Fahrenheit.")
Explanation: The conversion formula F=95C+32F = \frac{9}{5}C + 32F=59C+32 is used
here to convert the given Celsius temperature into Fahrenheit.
6. Python Program to Find the Roots of a Quadratic Equation
Question: Write a Python program to find the roots of a quadratic equation ax2+bx+c=0ax^2
+ bx + c = 0ax2+bx+c=0.
Solution:
import cmath # For complex square roots
# Taking user input for the coefficients
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
# Calculate discriminant
discriminant = cmath.sqrt(b**2 - 4*a*c)
# Calculate roots
root1 = (-b + discriminant) / (2*a)
root2 = (-b - discriminant) / (2*a)
print(f"The roots of the equation are: {root1} and {root2}")
Explanation: The program uses the quadratic formula to compute the roots. The cmath
library is used to handle complex roots.
7. Python Program to Calculate the Area of a Circle
Question: Write a Python program to calculate the area of a circle given its radius.
Solution:
import math # Importing the math module for Pi value
# Taking user input for the radius
radius = float(input("Enter the radius of the circle: "))
# Calculating the area
area = math.pi * radius**2
print(f"The area of the circle is: {area}")
Explanation: The area of a circle is calculated using the formula Area=π×radius2\text{Area}
= \pi \times \text{radius}^2Area=π×radius2, where math.pi gives the value of π.
8. Python Program to Solve a System of Linear Equations
Question: Solve the system of linear equations:
2x+3y=54x+y=62x + 3y = 5 \\ 4x + y = 62x+3y=54x+y=6
using Python.
Solution:
import numpy as np
# Coefficients matrix (A) and constant matrix (B)
A = np.array([[2, 3], [4, 1]])
B = np.array([5, 6])
# Solve the system of equations
solution = np.linalg.solve(A, B)
print(f"The solution is: x = {solution[0]}, y = {solution[1]}")
Explanation: The numpy.linalg.solve() function is used to solve the system of linear
equations using matrix operations.