Python Dersleri 2: Koşullu İfadeler ve Döngüler: Pratik Uygulamalarla Anlatım

Python Lessons 2: Conditional Expressions and Loops: Explanations with Practical Applications

Decision Making in Python: Conditional Expressions

Hello friends! I believe you have learned about Python's basic data types and variables. Now we will learn how to make decisions in Python and give our programs the ability to reason. This is possible with conditional statements .

By using a conditional statement, our program will take different paths depending on whether a certain condition is true or false.

The most common type of conditional statement is the if statement. The if statement checks the truth of a condition and executes the specified block of code if the condition is true.

Example: ```python age = int(input("Enter your age: ")) if age >= 18: print("You are an adult, so you can vote.") ```

In this example, the program takes the user's age and uses an if statement to check if the age is greater than or equal to 18. If the age is greater than or equal to 18, the message "You are an adult and therefore eligible to vote" is printed to the screen.

More Complex Decisions: else and elif

In addition to the if statement, we can also use else and elif statements.

The else statement specifies a block of code to be executed if the condition in the if statement is false. The elif statement is used to check multiple conditions.

Example: ```python grade = int(input("Enter your grade: ")) if grade >= 90: print("Congratulations! Your grade is excellent.") elif grade >= 80: print("A very good grade!") elif grade >= 70: print("A good grade.") else: print("You need to work harder.") ```

In this example, the program takes the user's grade and gives different messages depending on the grade value.

Repetition with loops: for and while

Loops are used in our program to run a specific block of code repeatedly. There are two types of loops in Python:

for loop and while loop. for loop is used to iterate over an array element.

Example: ```python for i in range(10): print(i) ```

In this example, the for loop iterates over the numbers 0 to 9 generated by the range(10) function and prints each number to the screen.

The while loop specifies a block of code to be executed as long as a condition is true.

Example: ```python counter = 0 while counter < 5: print(counter) counter += 1 ```

In this example, the while loop runs as long as the counter variable is less than 5. Inside the loop, the counter variable is incremented by 1 and printed to the screen.

Practical Applications

Now let's put what we learned into practice:

1. Get a Number from the User and Check if it is Even or Odd

```python number = int(input("Enter a number: ")) if number % 2 == 0: print(number "is an even number.") else: print(number "is an odd number.") ```

This program takes a number from the user and checks whether the number is even or odd using an if statement.

2. Adding Numbers from 1 to 10

```python total = 0 for i in range(1 11): total += i print("Sum of numbers from 1 to 10:" total) ```

This program uses a for loop to add numbers from 1 to 10 and print the total.

SUMMARY

In this lesson, we learned about conditional statements and loops in Python. We learned how to use if else elif for and while statements.

We reinforced these concepts with practical examples. Learning loops and conditionals is a very important step in your programming journey. Once you understand these topics well, you can write more complex and functional programs.

Now I'm excited to design your own programs and come up with creative solutions with these powerful tools that Python has to offer! Remember, programming is an adventure, a journey of discovery. Every new concept opens a new door. I am here to guide you on this journey.

Let's explore the world of Python together! In the next lesson we will learn about functions and lists. Get ready!

Back to blog

Leave a comment

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