0% found this document useful (0 votes)
10 views2 pages

Key Concepts in Python

Uploaded by

ayubuzuberi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Key Concepts in Python

Uploaded by

ayubuzuberi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Key Concepts in Python

1. Variables and Data Types


o Explanation: Variables store data, and Python supports various data types
such as integers, floats, strings, and booleans.
o Real-World Example: Storing a patient's name and age for a health record
system:

name = "Juma Shabani"


age = 32

2. Control Flow (Conditionals and Loops)


o Explanation: Control flow determines the execution path using if, elif, else
statements and loops (for, while).
o Real-World Example: A fitness app checks if a user's BMI is in a healthy
range:

if bmi < 18.5:


print("Underweight")
elif 18.5 <= bmi <= 24.9:
print("Healthy")
else:
print("Overweight")

3. Functions
o Explanation: Reusable blocks of code that perform a specific task, defined
with def.
o Real-World Example: A function to calculate BMI:

def calculate_bmi(weight, height):


return weight / (height ** 2)

4. Data Structures
o Explanation: Tools to organize and store data, including lists, tuples,
dictionaries, and sets.
o Real-World Example: Storing patient data in a dictionary:

patient = {"name": "Juma", "age": 28, "bmi": 23.5}

5. File Handling
o Explanation: Reading from and writing to files using Python's built-in open()
function.
o Real-World Example: Logging patient data to a file:

with open("patients.txt", "a") as file:


file.write(f"{patient['name']}, {patient['age']},
{patient['bmi']}\n")
6. Libraries and Modules
o Explanation: Predefined packages like NumPy, Pandas, and Matplotlib for
advanced operations.
o Real-World Example: Using Pandas to analyze health data:

import pandas as pd
data = pd.read_csv("health_data.csv")
print(data.describe())

7. Object-Oriented Programming (OOP)


o Explanation: Organizing code into objects using classes and methods.
o Real-World Example: Representing a patient as a class:

class Patient:
def __init__(self, name, age, bmi):
self.name = name
self.age = age
self.bmi = bmi

8. Error and Exception Handling


o Explanation: Managing errors using try and except blocks to make code
robust.
o Real-World Example: Handling incorrect user input:

try:
weight = float(input("Enter weight: "))
except ValueError:
print("Invalid input. Please enter a number.")

You might also like