Code With Python
39K subscribers
841 photos
24 videos
22 files
747 links
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Topic: Python Functions – Part 1 of 3: Basics, Syntax, and Parameters (Long Lesson)

---

### 1. What is a Function in Python?

A function is a reusable block of code that performs a specific task. Functions help:

• Avoid code duplication
• Improve code readability
• Enable modular programming

---

### 2. Why Use Functions?

Reusability – Write once, use many times
Modularity – Split large tasks into smaller blocks
Debuggability – Easier to test/debug small units
Abstraction – Hide complex logic behind a name

---

### 3. Function Syntax

def function_name(parameters):
# block of code
return result


---

### 4. Creating a Simple Function

def greet():
print("Hello, welcome to Python functions!")

greet() # Calling the function


---

### 5. Function with Parameters

def greet_user(name):
print(f"Hello, {name}!")

greet_user("Hussein")


---

### 6. Function with Return Value

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

result = add(10, 5)
print(result) # Output: 15


---

### 7. Positional vs Keyword Arguments

def student_info(name, age):
print(f"Name: {name}, Age: {age}")

student_info("Ali", 22) # Positional
student_info(age=22, name="Ali") # Keyword


---

### 8. Default Parameter Values

def greet(name="Guest"):
print(f"Hello, {name}!")

greet() # Output: Hello, Guest!
greet("Hussein") # Output: Hello, Hussein!


---

### 9. Variable Number of Arguments

#### \*args – Multiple positional arguments:

def sum_all(*numbers):
total = 0
for num in numbers:
total += num
return total

print(sum_all(1, 2, 3, 4)) # Output: 10


#### \*\*kwargs – Multiple keyword arguments:

def print_details(**info):
for key, value in info.items():
print(f"{key}: {value}")

print_details(name="Ali", age=24, country="Egypt")


---

### **10. Scope of Variables**

#### Local vs Global Variables

x = "global"

def func():
x = "local"
print(x)

func() # Output: local
print(x) # Output: global


Use global keyword if you want to modify a global variable inside a function.

---

### 11. Docstrings (Function Documentation)

def square(n):
"""Returns the square of a number."""
return n * n

print(square.__doc__) # Output: Returns the square of a number.


---

### 12. Best Practices

• Use descriptive names for functions
• Keep functions short and focused
• Avoid side effects unless needed
• Add docstrings for documentation

---

### Exercise

• Create a function that takes a list and returns the average
• Create a function that takes any number of scores and returns the highest
• Create a function with default arguments for greeting a user by name and language

---

#Python #Functions #CodingBasics #ModularProgramming #CodeReuse #PythonBeginners

https://t.iss.one/DataScience4
6👏2