Python Dersleri 3: Fonksiyonlar ve Listeler: Kodunuzu Daha Etkin ve Okunaklı Hale Getirme

Python Lessons 3: Functions and Lists: Making Your Code More Efficient and Readable

Organizing Our Code with Functions

Hello friends! We have learned basic data types, variables, conditional statements and loops in Python. Now we will learn how to use functions in Python to make our code more organized and reusable .

Functions are blocks of code that perform a specific task. Once you define a function, you can use it over and over again in different parts of your program. This makes your code shorter, more readable, and more manageable.

To define a function, we use the def keyword. The name of the function is enclosed in parentheses, followed by parameters (if any) and a colon. The function body is the rest of the code block and is indented.

Example: ```python def greet(name): """Gives the user a greeting message with name.""" print("Hello" name "!") greet("Ali") # Outputs "Hello Ali!" ```

In this example, we defined a function called greet() . The function takes a name parameter and prints a greeting message using that name. To call the function, we use the function name and the arguments in parentheses.

Returning Values ​​from Functions

Functions can return a value. We specify the value that a function will return by using the return keyword.

Example: ```python def add(number1 number2): """Adds two numbers and returns the result.""" total = number1 + number2 return total result = add(5 3) # assigns the value 8 to the result variable print(result) # returns 8 ```

In this example, we defined a function called sum() . The function takes two number parameters, adds them, and returns the sum. We specify the value that the function will return by using the return keyword. The value returned by the function is assigned to the result variable.

Organizing Data with Lists

Lists are sequential data structures in Python used to store multiple data. Lists are defined within square brackets and contain elements separated by commas.

Example: ```python fruits = ["apple" "banana" "strawberry"] ```

In this example, we created a list called fruits . The list contains three elements named "apple", "banana", and "strawberry".

Access List Elements

We can access list elements with their index numbers. Indexes start from 0.

Example: ```python fruits = ["apple" "banana" "strawberry"] print(fruits[0]) # outputs "apple" print(fruits[1]) # outputs "banana" ```

List Operations

We can perform operations such as adding, deleting, and changing elements in lists.

Example: ```python fruits = ["apple" "banana" "strawberry"] fruits.append("cherry") # adds the element "cherry" to the list fruits.remove("banana") # removes the element "banana" from the list fruits[1] = "grape" # replaces the second element with "grape" print(fruits) # outputs ["apple" "grape" "strawberry" "cherry"] ```

Practical Examples

Now let's put what we learned into practice:

1. Function that takes the user's name and prints a greeting message

```python def greet(name): """Gives the user a greeting message with their name.""" print("Hello" name "!") name = input("Your name: ") greet(name) # Prints a greeting message using the user's name ```

This program takes the name from the user and prints the greeting message using the greet() function.

2. Function to Find the Sum of Numbers in a List

```python def add(numbers): """Calculate the sum of the numbers in the list.""" total = 0 for number in numbers: total += number return total numbers = [1 2 3 4 5] result = add(numbers) # set the value 15 to the result variable print(result) # outputs 15 ```

This program calculates the sum of the numbers in the list using the sum() function and prints the result on the screen.

Summary

In this lesson, we learned about functions and lists in Python. We learned to define functions, call them, their parameters and return values. We learned to create lists, access their elements and list operations, making our codes more organized and effective.

Functions and lists are very important tools in the programming world. The better you understand them, the better your programs will be. Remember, programming is a journey of skill development. Each new concept means a new step.

I am here to guide you on this journey. Let's explore the magical world of Python together!

In the next lesson we will learn about dictionaries and file operations. Get ready!

Remember, try to approach the challenges you face while coding in a funny way. This will both increase your motivation and make your programming journey more fun. Having funny and entertaining moments in the programming world is very important. Because sometimes writing code can be boring and challenging. Sharing these memories allows us to motivate each other and increase our love of programming even more.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.