0% found this document useful (0 votes)
3 views12 pages

Lecture 2 Introduction To Python Programming

Introduction to python lab Eng/Mohammed Saleem Sana'a university
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Lecture 2 Introduction To Python Programming

Introduction to python lab Eng/Mohammed Saleem Sana'a university
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

 Sana'a University

Introduction to Python Programming


Lecture 2: Fundamentals for Biomedical Engineers

Course: Fundamentals of Programming

Instructor: Mohammed Saleem

Department: Biomedical Engineering

Sana'a University | Department of Biomedical Engineering | 2026


What is Python? History & Features

 Overview  Why Python for BME?

✓ High-level, interpreted, general-purpose language.  Powerful for Data Analysis & AI.

✓ Created by Guido van Rossum (1991).  Libraries: NumPy, Pandas, SciPy, Matplotlib.

⭐ Key Features # Simple Python Script


message = "Hello, Python!"
⚡ Simple & Readable Syntax
print(message)

 Cross-platform & Open Source


Python is the "lingua franca" of modern scienti c research and medical

 Massive Ecosystem of Libraries data science.


Se ing Up Your Environment

  ☁
O cial Python Anaconda Google Colab

Download from [Link]. The Recommended for BME! Includes Cloud-based Jupyter notebooks. No

standard installation for general use. Python + NumPy + Pandas + SciPy pre- installation required. Perfect for quick

installed. labs.

Popular IDEs (Editors)

 VS Code: Lightweight & Versatile  PyCharm: Professional Python IDE  Jupyter: Interactive Data Science

 Pro Tip: Use Anaconda Navigator to manage your libraries easily without using the terminal if you are a beginner.
Syntax Basics & Your First Program

 1. The "Hello World"  2. Core Syntax Rules

# My first Python script Indentation

print("Hello, World!") Python uses whitespace to de ne blocks. Crucial! Incorrect

indentation causes errors.

Comments
How to run it:
Use # for single-line notes. Python ignores these during execution.
 Save code as [Link]
 Open Terminal / Command Prompt
Case Sensitivity

〉 Type: python [Link] Variable and variable are NOT the same in Python.

# Example of Indentation
if True:
print("This is indented")
Variables and Basic Data Types

 Core Data Types  Practical Examples

Type Description Example


# Integer
int Whole numbers 10, -5
age = 21

oat Decimal numbers 37.5, -0.5 # Float (Biomedical Data)


temp = 36.8
str Text (Strings) "Ali", 'BME'

bool Logical values True, False


# String
patient = "Mohammed Saleem"

Variable Naming Rules:


# Boolean
is_stable = True
Must start with a le er or underscore

Can contain le ers, numbers, and underscores

Case-sensitive (age != Age)

# Check data type


print(type(temp)) # Output: <class 'float'>

# Multiple Assignment
x, y = 10, 20
Input and Output: Interacting with Programs

⌨ User Input  Program Output

The input() function reads data from the user. The print() function displays data on the screen.

# Basic input # Using f-strings (Recommended)


name = input("Enter name: ") bpm = 72
print(f"Heart Rate: {bpm} BPM")
# Input is ALWAYS a string
hr = input("Enter Heart Rate: ") # Multiple arguments
hr_int = int(hr) # Must convert! print("Status:", "Normal", "Stable")

# Formatting decimals
Note: Even if the user enters a number, input() returns it as text pi = 3.14159
(string). You must use int() or oat() for math. print(f"Value: {pi:.2f}")

f-strings allow you to embed variables directly inside strings using


curly braces {}.
Operators in Python

 Arithmetic = Comparison  Logical


+, -, *, / (Division) == (Equal), != (Not Equal) and: True if both are True
% (Modulus), ** (Power) > , < (Greater/Less) or: True if one is True
// (Floor Division) >= , <= (Greater/Less Equal) not: Reverses the state

a = 10 stable = True
b = 3 temp = 37.5 alert = False
print(a % b) # 1 is_fever = temp > 37.0 print(stable and alert) # False
print(a ** b) # 1000 print(is_fever) # True print(stable or alert) # True
print(a // b) # 3 print(10 == 10) # True print(not alert) # True
String Operations: Working with Text

 Concatenation

first = "John"
last = "Doe"
full = first + " " + last # Output: "John Doe"

✀ Slicing [start:end:step]  Common Methods


P Y T H O N
.upper() / .lower(): Change case

〉 .strip(): Remove whitespace

0 1 2 3 4 5 〉 .replace(old, new): Swap text

〉 .split(delim): Convert to list

data = "ECG_Signal_001"
print(data[0:3]) # "ECG" report = " BP: 120/80 "
print(data[4:10]) # "Signal" clean = [Link]() # "BP: 120/80"
print(data[-3:]) # "001" vals = [Link](": ") # ["BP", "120/80"]
Type Conversion: Changing Data Types

 Conversion Functions  Practical Examples

Function Converts to...


# String to Integer
s = "100"
int() Integer
n = int(s) # n is now 100

oat() Floating-point
# Float to Integer (Truncates)
pi = 3.14159
str() String (Text)
pi_int = int(pi) # pi_int is 3

bool() Boolean (T/F)


# Number to String
age = 25
msg = "Age: " + str(age)
⚠ Common Error:
Converting non-numeric text to numbers (e.g.,

int("Hello")) will cause a ValueError. # Verify with type()


print(type(n)) # <class 'int'>
Practice Exercises I: Test Your Skills

  
1. BMI Calculator 2. Temp Converter 3. ID Forma er

Ask the user for their weight (kg) and Ask the user for a temperature in Celsius Take a raw ID like " p_id_123_bme ",

height (m). Calculate and print their BMI. and convert it to Fahrenheit. remove spaces, uppercase it, and replace

'_' with '-'.

HINT: HINT: HINT:


BMI = weight / (height ** 2) F = (C * 9/5) + 32 Use .strip(), .upper(), and .replace()

Remember to convert input to oat! Use f-strings for clean output. methods.
Practice Exercises II & Challenge

 Exercise 4  Advanced Challenge

Signal Data Extractor Medical Device Data Parser

Given the following sensor string, extract the Heart Rate Parse the following raw data string from a patient monitor:

(HR) and Blood Pressure (BP) values using string slicing or

spli ing. raw = "DEVICE:ECG;PATIENT:P001;HR:72;TEMP:37.1"

data = "SensorData_HR_75_BP_120_80"
 Extract Patient ID , HR, and TEMP .

 Convert HR to integer and TEMP to oat.


Goal: Print "HR: 75" and "BP: 120/80".

 Print a forma ed summary report.

# Hint: Use .split(";") then .split(":")


parts = [Link](";")
Exercise 5: Simple Calculator hr_val = parts[2].split(":")[1]

Ask the user for two numbers and an operator (+, -, *, /).

Perform the operation and print the result.


Summary and What's Next

 Lecture 2 Recap

→ Coming Up: Lecture 3


 Python Fundamentals: History, setup, and IDEs.

 Control Structures: Making decisions with


 Core Syntax: Indentation, comments, and case sensitivity.
if/elif/else.
 Data Handling: Variables, types (int, oat, str, bool).
↻ Loops: Repeating tasks with for and while.
 Interaction: User input and forma ed output (f-strings).
 Functions: Modularizing and reusing your code.

 Operations: Arithmetic, comparison, and string manipulation.

Don't forget to complete the practice exercises!

"The only way to learn a new programming language is by writing programs in it."

— Dennis Ritchie

You might also like