From 83de0320bf1c11cb3c5147e213c756b8c6535dc1 Mon Sep 17 00:00:00 2001 From: codeasarjun Date: Sun, 11 Aug 2024 16:37:41 +0530 Subject: [PATCH] data structure has been added! --- 02-control-flow/README.md | 11 +++++++++ 02-control-flow/conditions.py | 16 +++++++++++++ 02-control-flow/loops.py | 10 ++++++++ 03-data-structures/README.md | 38 ++++++++++++++++++++++++++++++ 03-data-structures/dictionaries.py | 20 ++++++++++++++++ 03-data-structures/lists.py | 16 +++++++++++++ 03-data-structures/sets.py | 22 +++++++++++++++++ 03-data-structures/tuples.py | 10 ++++++++ 8 files changed, 143 insertions(+) create mode 100644 02-control-flow/README.md create mode 100644 02-control-flow/conditions.py create mode 100644 02-control-flow/loops.py create mode 100644 03-data-structures/README.md create mode 100644 03-data-structures/dictionaries.py create mode 100644 03-data-structures/lists.py create mode 100644 03-data-structures/sets.py create mode 100644 03-data-structures/tuples.py diff --git a/02-control-flow/README.md b/02-control-flow/README.md new file mode 100644 index 0000000..871dee8 --- /dev/null +++ b/02-control-flow/README.md @@ -0,0 +1,11 @@ +# Control Flow in Python + +## Introduction + +In this section, we will explore the control flow mechanisms in Python, including conditional statements and loops. + +## 1. Condition + In Python, we use `if`, `elif`, and `else` statements to implement conditional logic. + +## 2. Control Flow + Loops are used to execute a block of code repeatedly until a certain condition is met. diff --git a/02-control-flow/conditions.py b/02-control-flow/conditions.py new file mode 100644 index 0000000..3dfd8a6 --- /dev/null +++ b/02-control-flow/conditions.py @@ -0,0 +1,16 @@ +def check_number(num): + """Check if a number is positive, negative, or zero.""" + if num > 0: + return "The number is positive." + elif num < 0: + return "The number is negative." + else: + return "The number is zero." +number = 10 +print(f"Number: {number} - {check_number(number)}") + +number = -5 +print(f"Number: {number} - {check_number(number)}") + +number = 0 +print(f"Number: {number} - {check_number(number)}") diff --git a/02-control-flow/loops.py b/02-control-flow/loops.py new file mode 100644 index 0000000..52f819c --- /dev/null +++ b/02-control-flow/loops.py @@ -0,0 +1,10 @@ +fruits = ["apple", "banana", "cherry"] +print("Fruits in the list:") +for fruit in fruits: + print(fruit) + +count = 1 +print("\nCounting from 1 to 5:") +while count <= 5: + print(count) + count += 1 diff --git a/03-data-structures/README.md b/03-data-structures/README.md new file mode 100644 index 0000000..f0a0f58 --- /dev/null +++ b/03-data-structures/README.md @@ -0,0 +1,38 @@ +We will explore four fundamental data structures available in Python: lists, tuples, dictionaries, and sets. Understanding these data structures is essential for effective data management and manipulation in Python programming. + +## 1. Lists + +Lists are ordered collections that can hold multiple items of various types. They are mutable, meaning you can change their contents after they are created. + +### Features +- **Ordered**: Items have a defined order and maintain that order. +- **Mutable**: You can add, remove, or modify items. +- **Indexed**: Items are accessed by their index, starting from 0. + +## 2. Tuples + +Tuples are ordered collections similar to lists but are immutable, which means once they are created, their contents cannot be changed. + +### Features +- **Ordered**: Items have a defined order. +- **Immutable**:We cannot add, modify, or remove items after creation. +- **Indexed**: Items are accessed by their index, starting from 0. + +## 3. Dictionaries + +Dictionaries are unordered collections of key-value pairs. Each key is unique and is used to access the corresponding value. + +### Features +- **Unordered**: Items do not maintain a specific order. +- **Mutable**: You can add, modify, or remove key-value pairs. +- **Key-Value Pair**: Each item consists of a key and a corresponding value. + + +## 4. Sets + +Sets are unordered collections of unique elements. They are useful for operations such as membership testing, eliminating duplicates, and performing set operations. + +### Features +- **Unordered**: Items do not maintain any specific order. +- **Mutable**: You can add or remove items, but items must be unique. +- **Unique Elements**: Duplicate elements are automatically removed. diff --git a/03-data-structures/dictionaries.py b/03-data-structures/dictionaries.py new file mode 100644 index 0000000..a1122af --- /dev/null +++ b/03-data-structures/dictionaries.py @@ -0,0 +1,20 @@ + +person = { + "name": "Alice", + "age": 30, + "city": "New York" +} + +print("Name:", person["name"]) + +person["email"] = "test@example.com" +print("Dictionary after adding an email:", person) + +del person["city"] +print("Dictionary after removing city:", person) + +print("Dictionary keys and values:") +for key, value in person.items(): + print(f"{key}: {value}") + +print("Email:", person.get("email", "Not found")) diff --git a/03-data-structures/lists.py b/03-data-structures/lists.py new file mode 100644 index 0000000..668fb1f --- /dev/null +++ b/03-data-structures/lists.py @@ -0,0 +1,16 @@ +fruits = ["apple", "banana", "cherry"] + +print("First fruit:", fruits[0]) + +fruits.append("date") +print("List after adding a fruit:", fruits) + +fruits.remove("banana") +print("List after removing a fruit:", fruits) + +print("All fruits in the list:") +for fruit in fruits: + print(fruit) + + +print("First two fruits:", fruits[:2]) diff --git a/03-data-structures/sets.py b/03-data-structures/sets.py new file mode 100644 index 0000000..acb8014 --- /dev/null +++ b/03-data-structures/sets.py @@ -0,0 +1,22 @@ + +numbers = {1, 2, 3, 4, 5} + +numbers.add(6) +print("Set after adding an element:", numbers) + +numbers.remove(3) +print("Set after removing an element:", numbers) + +numbers.add(5) +print("Set after adding a duplicate element:", numbers) + +print("All numbers in the set:") +for number in numbers: + print(number) + +set_a = {1, 2, 3} +set_b = {3, 4, 5} + +print("Union of sets:", set_a | set_b) +print("Intersection of sets:", set_a & set_b) +print("Difference of sets:", set_a - set_b) diff --git a/03-data-structures/tuples.py b/03-data-structures/tuples.py new file mode 100644 index 0000000..95a25d8 --- /dev/null +++ b/03-data-structures/tuples.py @@ -0,0 +1,10 @@ +coordinates = (10, 20, 30) + +print("First coordinate:", coordinates[0]) + + +print("All coordinates:") +for coordinate in coordinates: + print(coordinate) + +print("First two coordinates:", coordinates[:2])