Lädt...


🔧 Day 2: Python Control Structures, Functions, Modules, and Data Structures


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Day 2: Python Control Structures, Functions, Modules, and Data Structures

Welcome to Day 2! Today, we’ll not only wrap up Python’s control structures but also explore functions, modules, and fundamental data structures. By the end, you’ll be equipped to build efficient, reusable, and organized code. Let’s get started!

Python Control Structures Recap

We learned how if, elif, and else help us make decisions and how loops (for and while) help repeat tasks. Here's a quick practice problem for reinforcement:

Challenge: Write a program that checks whether numbers from 1 to 10 are odd or even.

for i in range(1, 11):
    if i % 2 == 0:
        print(f"{i} is even.")
    else:
        print(f"{i} is odd.")

Functions in Python

Functions are blocks of reusable code that perform specific tasks.

1. Defining and Calling Functions

def greet(name):
    return f"Hello, {name}!"

print(greet("Arjun"))
  • Defining: Use def followed by the function name and parameters.
  • Calling: Use the function name with arguments to execute it.

2. Function Arguments and Return Values

  • Arguments: Input values passed to the function.
  • Return Values: Results returned by the function.

Example:

def add_numbers(a, b):
    return a + b

result = add_numbers(5, 3)
print(f"The sum is {result}.")

Modules in Python

Modules are collections of functions and variables. Python has built-in modules, and you can create your own.

1. Using Built-In Modules

import math
import random

print(math.sqrt(16))  # Square root of 16
print(random.randint(1, 10))  # Random number between 1 and 10

2. Writing Your Own Module

Save the following in a file named calculator.py:

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

Use it in another script:

from calculator import add, subtract

print(add(10, 5))  # Output: 15
print(subtract(10, 5))  # Output: 5

Data Structures in Python

Python provides versatile data structures like lists, tuples, sets, and dictionaries for managing data.

1. Lists

A list is a collection of ordered, mutable items.

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits[1])  # Access item at index 1

2. Tuples

Tuples are immutable lists.

dimensions = (10, 20, 30)
print(dimensions[0])  # Access item at index 0

3. Sets

Sets are unordered collections of unique items.

numbers = {1, 2, 3, 3}
numbers.add(4)
print(numbers)  # Output: {1, 2, 3, 4}

4. Dictionaries

Dictionaries store key-value pairs.

user = {"name": "Alice", "age": 25}
print(user["name"])  # Access value by key

Practice Example: Real-World Application

Create a dictionary to store and retrieve user information:

users = {}
for _ in range(3):
    name = input("Enter your name: ")
    age = int(input("Enter your age: "))
    users[name] = age

for name, age in users.items():
    print(f"{name} is {age} years old.")

Conclusion

Today, we:

  1. Wrapped up control structures.
  2. Explored the power of functions and learned to create reusable code.
  3. Leveraged modules for efficiency, including writing custom ones.
  4. Learned about Python's versatile data structures.

Practice these concepts thoroughly, as they form the backbone of Python programming. Tomorrow, we’ll delve into file handling and exception management to take your skills further. 🚀

...

🔧 Day 2: Python Control Structures, Functions, Modules, and Data Structures


📈 61.05 Punkte
🔧 Programmierung

🔧 What are Data Structures? Data Structures and Algorithms Day #1


📈 33.51 Punkte
🔧 Programmierung

🔧 Functions of Commercial Bank: Primary Functions and Secondary Functions


📈 29.14 Punkte
🔧 Programmierung

🔧 Python Functions and Modules: Writing Reusable Code Like a Pro


📈 26.98 Punkte
🔧 Programmierung

🔧 Week 1: Building Blocks of C Programming – A Journey Through Control Structures, Loops, and Functions


📈 26.54 Punkte
🔧 Programmierung

🔧 Week 1: Building Blocks of C Programming – A Journey Through Control Structures, Loops, and Functions


📈 26.54 Punkte
🔧 Programmierung

🔧 First-Class Functions, Higher-Order Functions, and Closures in Python – Explained with Code Examples


📈 24.97 Punkte
🔧 Programmierung

🔧 Practical C++20 Modules and the future of tooling around C++ Modules with Cameron DaCamara


📈 23.87 Punkte
🔧 Programmierung

🔧 Durable functions in Python for Azure Functions | Azure Friday


📈 23.72 Punkte
🔧 Programmierung

🔧 45-Day Data Structures and Algorithms Roadmap (2 hours/day) using JavaScript


📈 23.21 Punkte
🔧 Programmierung

🕵️ GitHub - ZehMatt/zasm-modules: Generating binary modules with zasm


📈 22.62 Punkte
🕵️ Reverse Engineering

🕵️ CVE-2024-25298 | REDAXO 5.15.1 modules.modules.php information disclosure


📈 22.62 Punkte
🕵️ Sicherheitslücken

🔧 TS1103: 'for await' loops are only allowed within async functions and at the top levels of modules


📈 21.85 Punkte
🔧 Programmierung

🔧 Stack and Queue || Python || Data Structures and Algorithms


📈 21.81 Punkte
🔧 Programmierung

🔧 Day 4: Modules and pip in python


📈 21.57 Punkte
🔧 Programmierung

🔧 Day 3: Modules and Pip | 100 Days Python


📈 21.57 Punkte
🔧 Programmierung

🔧 Python Day - 8 Modules-Meaning and Types,Tasks


📈 21.57 Punkte
🔧 Programmierung

🎥 Python For Ethical Hacking - #1 - Introduction & Python Modules


📈 21.56 Punkte
🎥 IT Security Video

🔧 Python Day 2 -Functions-Meaning & types, Data type


📈 21.06 Punkte
🔧 Programmierung

🎥 Install Node Modules in your Azure Functions application [8 of 16] | Beginner's Series to Serverless


📈 20.6 Punkte
🎥 Video | Youtube

🔧 The Power of Python Data Structures: Lists, Dictionaries, Sets and Tuples


📈 20.56 Punkte
🔧 Programmierung

🔧 Dive into Data Structures and Algorithms with Python 🐍


📈 20.56 Punkte
🔧 Programmierung

🔧 Binary Search || Python || Data Structures and Algorithms


📈 20.56 Punkte
🔧 Programmierung

🔧 Day 9: Python Built In Modules


📈 20.32 Punkte
🔧 Programmierung

🔧 Python Day-9 Predefined modules,While loop,Task


📈 20.32 Punkte
🔧 Programmierung

🔧 Day 4: Control Structures in JavaScript


📈 19.88 Punkte
🔧 Programmierung

🔧 Durable Functions 2.0 - Serverless Actors, Orchestrations, and Stateful Functions


📈 19.84 Punkte
🔧 Programmierung

🔧 The difference between Arrow functions and Normal functions in JavaScript


📈 19.84 Punkte
🔧 Programmierung

matomo