Skip to content

Commit f31f391

Browse files
Add files via upload
1 parent 12858ea commit f31f391

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

week_7/01-Function.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Functions in Python
2+
3+
Functions are blocks of reusable code designed to perform specific tasks. They make code more modular, organized, and easier to read and maintain. The code inside the body of a function is executed only when the function is called.
4+
5+
In Python, functions can be broadly categorized into two types: predefined functions and user-defined functions.
6+
7+
### 1. Predefined Functions
8+
9+
Predefined functions, also known as built-in functions, are functions that are already available in Python. These functions are provided by Python's standard library and can be used without the need for any additional code or imports. Common examples include:
10+
11+
- **`print()`**: Outputs data to the console.
12+
- **`len()`**: Returns the length of an object.
13+
- **`type()`**: Returns the type of an object.
14+
15+
### 2. User-Defined Functions
16+
17+
User-defined functions are functions created by the programmer to perform specific tasks. These functions are not built into Python; instead, they are defined by the user to handle tasks unique to their program or application.
18+
19+
### Creating a Function
20+
21+
In Python, a function is defined using the `def` keyword:
22+
23+
```python
24+
def my_function():
25+
print("Hello from a function")
26+
```
27+
### Calling a Function
28+
29+
```python
30+
def my_function():
31+
print("Hello from a function")
32+
33+
my_function() # Output: Hello from a function
34+
35+
```
36+
## Practice Questions for Functions
37+
38+
### 1. Sum of Two Numbers
39+
40+
Write a Python function `sum_two_numbers` that takes two arguments and returns their sum.
41+
42+
### 2. Maximum of Three Numbers
43+
44+
Write a Python function `max_of_three` that takes three numbers as arguments and returns the largest of the three.
45+
46+
### 3. Fibonacci Sequence
47+
48+
Write a Python function `fibonacci` that takes an integer `n` and returns the `n`th number in the Fibonacci sequence.
49+
50+
### 4. Factorial of a Number
51+
52+
Write a Python function `factorial` that takes an integer `n` and returns the factorial of `n` (i.e., `n!`).
53+
54+
### 5. Check Prime Number
55+
56+
Write a Python function `is_prime` that takes an integer `n` and returns `True` if `n` is a prime number, and `False` otherwise.
57+
58+
### 6. Reverse a String
59+
60+
Write a Python function `reverse_string` that takes a string `s` and returns the reversed version of the string.

week_7/02-Arguments.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### Arguments
2+
3+
Information can be passed into functions as arguments.
4+
5+
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
6+
7+
The following example has a function with one argument (`fname`). When the function is called, we pass along a first name, which is used inside the function to print the full name:
8+
9+
```python
10+
def my_function(fname):
11+
print(fname + " Refsnes")
12+
13+
my_function("Emil") # Output: Emil Refsnes
14+
my_function("Tobias") # Output: Tobias Refsnes
15+
my_function("Linus") # Output: Linus Refsnes
16+
```
17+
Arguments are often shortened to args in Python documentation.
18+
19+
### Parameters or Arguments?
20+
21+
The terms parameter and argument can be used for the same thing: information that is passed into a function.

week_7/eight.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#createing a function
2+
3+
def greet():
4+
print("Hello, World!")
5+
6+
#Calling A Function:
7+
greet()
8+
9+
10+
#Parematerized Function
11+
12+
def add(a, b):
13+
return a + b
14+
15+
result = add(5, 3)
16+
print(result) # Output: 8

0 commit comments

Comments
 (0)