Introduction to Python & Script Model Programming
Python is a high-level, interpreted, object-oriented programming language widely
used in engineering, data analysis, automation, and scientific computing.
Why Python for Civil Engineers?
Python is used in:
Structural Analysis data processing (ETABS/SAP output handling)
Quantity take-o automation
Survey data computation
GIS automation (QGIS)
Numerical Methods (Matrix operations)
Soil & Material test data analysis
Plotting stress-strain curves
Script Model Programming
Python follows Script-based execution model:
Code written in .py file
Executed line-by-line
No compilation required
Errors shown at runtime
Advantages in Civil Engineering:
Quick calculations
Rapid prototyping
Easy debugging
Automation of repetitive calculations
Example:
print("Welcome to Civil Engineering Programming")
Python Variables
A variable stores data in memory.
Characteristics:
No need to declare data type
Dynamic typing
Case sensitive
Example:
length = 10
breadth = 5
area = length * breadth
Civil Engineering Application:
Storing dimensions
Storing load values
Storing material properties
Basic Operators
3.1 Arithmetic Operators
Operator Meaning Civil Example
+ Addition Total Load
- Subtraction Net Load
* Multiplication Area = L × B
/ Division Stress = Load / Area
** Power x² in equations
Example:
stress = load / area
3.2 Comparison Operators
Used in safety checks:
if actual_stress < permissible_stress:
print("Safe")
3.3 Logical Operators
Operator Meaning
and Both conditions true
or Any condition true
not Reverse condition
Used in:
Structural design checks
Condition-based validation
Numeric Data Types
4.1 int
Whole numbers
Example: Number of floors
4.2 float
Decimal numbers
Example: Slab thickness = 0.125 m
4.3 complex
Used in structural vibration analysis
Example:
z = 3 + 4j
String Data Type
Strings store text data.
Example:
project_name = "Girls Hostel G+2"
String Operations:
Concatenation
Slicing
Replace
Split
Upper/Lower conversion
Civil Example:
Storing project name
Generating report headers
Naming files
Lists
A list stores multiple values.
beam_lengths = [3.5, 4.2, 5.1]
Characteristics:
Ordered
Mutable
Allows duplicate values
6.1 List Slicing
beam_lengths[0:2]
6.2 List Built-in Methods
Method Use
append() Add value
remove() Remove value
pop() Remove last
sort() Arrange
Method Use
len() Length
Civil Application:
Store multiple loads
Store test results
Store floor heights
Tuple
Tuple is similar to list but immutable.
coordinates = (19.0760, 72.8777)
Used in:
GPS Coordinates
Fixed dimensions
Structural constants
Dictionary
Stores data in key-value format.
material = {"Cement": 50, "Sand": 100}
Features:
Unordered
Mutable
Unique keys
Methods:
keys()
values()
items()
update()
Civil Application:
Material quantity storage
Student records
Site data mapping
Program Flow Control
9.1 Conditional Statements
if
if-else
if-elif-else
Example:
if load > capacity:
print("Unsafe")
elif load == capacity:
print("At Limit")
else:
print("Safe")
Used in:
Structural safety check
Soil bearing capacity check
Loops in Python
10.1 For Loop
for i in range(5):
print(i)
For Loop with List
for length in beam_lengths:
print(length)
For Loop with Dictionary
for key, value in [Link]():
print(key, value)
Civil Use:
Multiple beam analysis
Summation of loads
Iteration of data points
Functions
Function is reusable code block.
def calculate_area(l, b):
return l * b
Advantages:
Code reusability
Reduces repetition
Modular programming
Civil Applications:
Stress calculation
Volume calculation
Reinforcement area calculation
11.1 Lambda Function
Small one-line function.
area = lambda l, b: l*b
Used in:
Quick calculations
Sorting operations
Modules and Packages
Module:
Single Python file
Example:
import math
Package:
Collection of modules.
External Packages:
NumPy
Matplotlib
Pandas
NumPy (Numerical Python)
Used for:
Matrix operations
Structural FEM calculations
Large numerical datasets
Arrays
import numpy as np
arr = [Link]([10, 20, 30])
Advantages over List:
Faster
Memory e icient
Supports vector operations
Array Indexing
arr[0]
Matplotlib
Used for graphical representation.
import [Link] as plt
[Link](x,y)
[Link]()
Civil Applications:
Stress-Strain curve
Load vs Deflection graph
Survey profile plotting
Subplots
[Link](2,1,1)
Used to compare:
Multiple soil samples
Di erent load cases
Images
Used in:
GIS raster data
Contour maps
Satellite imagery
File Operations in Python
Reading File
file = open("[Link]","r")
content = fi[Link]()
fi[Link]()
Writing File
file = open("[Link]","w")
fi[Link]("Design Completed")
fi[Link]()
File Modes
Mode Meaning
r Read
w Write
a Append
Civil Applications:
Reading configuration files
Writing analysis reports
Logging structural calculations
Saving quantity estimation results
Programming Using File Operations
Example:
Read load data from file
Calculate stress
Save output to report file
Library, Package & Module Di erence
Term Meaning Example
Module Single file [Link]
Package Folder of modules NumPy
Library Collection of packages Python Standard Library
Overall Importance in Civil Engineering (SPPU Perspective)
Python enables:
Automation of repetitive calculations
E icient handling of large data
Structural matrix computations
Research-oriented programming
GIS data handling
Survey data analysis
Report generation