Python | Algorithms | Data Structures | Cyber ​​Security | Networks
38.6K subscribers
776 photos
23 videos
21 files
711 links
This channel is for Programmers, Coders, Software Engineers.

1) Python
2) django
3) python frameworks
4) Data Structures
5) Algorithms
6) DSA

Admin: @Hussein_Sheikho

Ad & Earn money form your channel:
https://telega.io/?r=nikapsOH
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
#Python #Top60 #BuiltInFunctions

#1. print()
Prints the specified message to the screen.

print("Hello, World!")

Hello, World!


#2. len()
Returns the number of items in an object.

my_list = [1, 2, 3, 4]
print(len(my_list))

4


#3. type()
Returns the type of an object.

name = "Python"
print(type(name))

<class 'str'>


#4. input()
Allows user input.

username = input("Enter your name: ")
print("Hello, " + username)

Enter your name: Alex
Hello, Alex


#5. int()
Converts a value to an integer number.

string_number = "101"
number = int(string_number)
print(number + 9)

110

---
#Python #DataTypes #Conversion

#6. str()
Converts a value to a string.

age = 25
print("My age is " + str(age))

My age is 25


#7. float()
Converts a value to a floating-point number.

integer_value = 5
print(float(integer_value))

5.0


#8. bool()
Converts a value to a Boolean (True or False).

print(bool(1))
print(bool(0))
print(bool("Hello"))
print(bool(""))

True
False
True
False


#9. list()
Converts an iterable (like a tuple or string) to a list.

my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list)

[1, 2, 3]


#10. tuple()
Converts an iterable to a tuple.

my_list = [4, 5, 6]
my_tuple = tuple(my_list)
print(my_tuple)

(4, 5, 6)

---
#Python #Math #Functions

#11. sum()
Returns the sum of all items in an iterable.

numbers = [10, 20, 30]
print(sum(numbers))

60


#12. max()
Returns the largest item in an iterable.

numbers = [5, 29, 12, 99]
print(max(numbers))

99


#13. min()
Returns the smallest item in an iterable.

numbers = [5, 29, 12, 99]
print(min(numbers))

5


#14. abs()
Returns the absolute (positive) value of a number.

negative_number = -15
print(abs(negative_number))

15


#15. round()
Rounds a number to a specified number of decimals.

pi = 3.14159
print(round(pi, 2))

3.14

---
#Python #Iterables #Functions

#16. range()
Returns a sequence of numbers, starting from 0 by default, and increments by 1.

for i in range(5):
print(i)

0
1
2
3
4


#17. sorted()
Returns a new sorted list from the items in an iterable.

unsorted_list = [3, 1, 4, 1, 5, 9]
sorted_list = sorted(unsorted_list)
print(sorted_list)

[1, 1, 3, 4, 5, 9]


#18. enumerate()
Returns an enumerate object, which contains pairs of index and value.

fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(index, fruit)

0 apple
1 banana
2 cherry


#19. zip()
Returns an iterator that aggregates elements from two or more iterables.

names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old.")

Alice is 25 years old.
Bob is 30 years old.
Charlie is 35 years old.


#20. map()
Applies a given function to each item of an iterable and returns a map object.