0% found this document useful (0 votes)
4 views9 pages

Python Notes

This document provides a comprehensive overview of Python programming basics, covering key concepts such as printing output, variables, data types, control flow, loops, functions, and data structures like lists and dictionaries. It also includes a practical example of a Python program for managing a fish pond's water levels, demonstrating user interaction and basic calculations. Each section is accompanied by code snippets to illustrate the concepts effectively.
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)
4 views9 pages

Python Notes

This document provides a comprehensive overview of Python programming basics, covering key concepts such as printing output, variables, data types, control flow, loops, functions, and data structures like lists and dictionaries. It also includes a practical example of a Python program for managing a fish pond's water levels, demonstrating user interaction and basic calculations. Each section is accompanied by code snippets to illustrate the concepts effectively.
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
You are on page 1/ 9

‭ ere are some clear and concise notes on the‬‭basics‬‭of Python programming‬‭, along with‬

H
‭examples‬‭to illustrate each concept.‬

‭🐍 Python Programming Basics‬


‭1. Printing Output‬

print()‬‭function is used to display output.‬


‭The‬‭

‭print("Hello, World!")‬

‭2. Variables and Data Types‬

‭Python is dynamically typed, meaning you don’t need to declare data types.‬

‭ ame = "Alice"
n # String‬
‭age = 25 # Integer‬
‭height = 5.6 # Float‬
‭is_student = True # Boolean‬

‭3. Comments‬

‭Used to explain code. Python ignores comments.‬

‭# This is a single-line comment‬

"‭ ""‬
‭This is a‬
‭multi-line comment‬
‭"""‬

‭4. Basic Input‬

input()‬‭is used to get user input.‬



‭ ame = input("Enter your name: ")‬
n
‭print("Hello, " + name)‬

‭5. Type Conversion‬

‭Convert between data types using built-in functions.‬

‭ ge = int(input("Enter your age: "))‬


a
‭print("You are", age, "years old.")‬

‭6. Operators‬

+ - * / % // **‬
‭Arithmetic:‬‭
== != > < >= <=‬
‭Comparison:‬‭
and or not‬
‭Logical:‬‭

‭ = 10‬
x
‭y = 3‬
‭print(x + y) # 13‬
‭print(x ** y) # 1000 (10^3)‬

‭7. Control Flow‬

if‬‭Statements:‬

‭ ge = 18‬
a
‭if age >= 18:‬
‭print("You are an adult.")‬
‭else:‬
‭print("You are a minor.")‬

elif‬
‭ ‭:‬
‭ core = 85‬
s
‭if score >= 90:‬
‭print("Grade A")‬
‭elif score >= 80:‬
‭print("Grade B")‬
‭else:‬
‭print("Grade C")‬

‭8. Loops‬

for‬‭Loop:‬

‭for i in range(5):‬
‭print(i) # 0 to 4‬

while‬‭Loop:‬

i‭ = 0‬
‭while i < 5:‬
‭print(i)‬
‭i += 1‬

‭9. Functions‬

def‬‭keyword.‬
‭Defined using the‬‭

‭def greet(name):‬
‭print("Hello", name)‬

‭greet("Alice")‬

‭10. Lists‬

‭Ordered, mutable collections.‬

f‭ruits = ["apple", "banana", "cherry"]‬


‭print(fruits[0]) # apple‬
‭fruits.append("date")‬
‭print(fruits)‬

‭11. Dictionaries‬

‭Key-value pairs.‬
‭person = {‬
‭"name": "Alice",‬
‭"age": 25‬
‭}‬
‭print(person["name"])‬

‭12. Tuples‬

‭Immutable sequences.‬

‭ oint = (3, 4)‬


p
‭print(point[0]) # 3‬

‭13. Sets‬

‭Unordered collections with no duplicates.‬

‭ olors = {"red", "green", "blue"}‬


c
‭colors.add("yellow")‬

‭14. Exception Handling‬


‭try:‬
‭x = int(input("Enter a number: "))‬
‭except ValueError:‬
‭print("That's not a valid number!")‬

‭Sure! Here's an additional note about‬‭appending to‬‭a list‬‭, to be added under the‬‭Lists‬‭section:‬

‭📌 Appending to a List‬

append()‬‭method.‬
‭You can add items to the end of a list using the‬‭

‭🔹 Syntax:‬

‭list_name.append(item)‬
‭🔹 Example:‬

‭fruits = ["apple", "banana"]‬

‭fruits.append("cherry")‬

‭print(fruits)‬

‭Output:‬

‭['apple', 'banana', 'cherry']‬

append()‬‭call adds one item to the‬‭end‬‭of the‬‭list. To add multiple items, use‬‭
‭Each‬‭ extend()‬
+=‬‭instead.‬
‭or‬‭

‭🔹 Example with‬‭
extend()‬
‭:‬

‭fruits.extend(["date", "elderberry"])‬

‭print(fruits)‬

‭Output:‬

‭['apple', 'banana', 'cherry', 'date', 'elderberry']‬

‭Here are detailed notes and a‬‭Python program example‬‭to help you understand how to:‬


‭ Calculate the‬‭volume‬‭and‬‭surface area‬‭of a fish‬‭pond‬

‭ Add or remove water based on user input‬

‭ Keep track of the current water level‬

‭🐟 Python Program: Fish Pond Water Management‬


‭💡 Step-by-Step Breakdown‬

‭1. Define the Pond Dimensions‬

‭Assume a‬‭rectangular‬‭or‬‭cylindrical‬‭pond. Here, we’ll‬‭use a rectangular pond for simplicity.‬

‭length = 10 # in meters‬

‭width = 5 # in meters‬

‭depth = 2 # in meters‬

‭2. Calculate Volume and Area‬

‭volume = length * width * depth # in cubic meters‬

‭surface_area = length * width # top surface area in square meters‬

‭print("Pond volume:", volume, "m³")‬

‭print("Pond surface area:", surface_area, "m²")‬

‭3. Track Current Water Volume‬

‭current_water = 0 # starts empty‬

‭4. Functions to Add or Remove Water‬

‭def add_water(amount):‬

‭global current_water‬
‭if current_water + amount <= volume:‬

‭current_water += amount‬

‭print(f"{amount} m³ of water added.")‬

‭else:‬

‭print("Cannot add water: exceeds pond capacity.")‬

‭def remove_water(amount):‬

‭global current_water‬

‭if current_water - amount >= 0:‬

‭current_water -= amount‬

‭print(f"{amount} m³ of water removed.")‬

‭else:‬

‭print("Cannot remove water: not enough water.")‬

‭5. User Menu for Interaction‬

‭def menu():‬

‭while True:‬

‭print("\n--- Fish Pond Menu ---")‬

‭print("1. Add water")‬

‭print("2. Remove water")‬

‭print("3. Show current water level")‬

‭print("4. Exit")‬
‭choice = input("Choose an option (1-4): ")‬

‭if choice == '1':‬

‭amount = float(input("Enter amount of water to add (m³): "))‬

‭add_water(amount)‬

‭elif choice == '2':‬

‭amount = float(input("Enter amount of water to remove (m³): "))‬

‭remove_water(amount)‬

‭elif choice == '3':‬

‭print(f"Current water level: {current_water} m³")‬

‭elif choice == '4':‬

‭print("Exiting program.")‬

‭break‬

‭else:‬

‭print("Invalid option. Please choose again.")‬

‭6. Run the Program‬

‭if __name__ == "__main__":‬

‭print("Welcome to the Fish Pond Manager")‬

‭print(f"Pond capacity: {volume} m³")‬

‭menu()‬
‭📝 Notes:‬

‭●‬ ‭1 cubic meter (m³) = 1000 liters of water.‬

volume =‬
‭●‬ ‭You can modify dimensions or extend to other shapes (like cylindrical using‬‭
π × r² × h‬
‭ ‭).‬

‭●‬ G
‭ lobal variables are used here for simplicity; you can encapsulate in a class for better‬
‭structure.‬

You might also like