kristofer revidoval tento gist . Přejít na revizi
Žádné změny
kristofer revidoval tento gist . Přejít na revizi
1 file changed, 86 insertions
ifsloopspy.md(vytvořil soubor)
@@ -0,0 +1,86 @@ | |||
1 | + | ## if statements and loops : the building blocks of your programming | |
2 | + | ||
3 | + | Here’s a concise explanation of **if statements** and **loops** in Python that might serve as an introductory overview: | |
4 | + | ||
5 | + | ### Introduction to If Statements and Loops | |
6 | + | ||
7 | + | Python is a programming language that uses control structures like **if statements**, loops, and conditional expressions. These constructs allow programmers to make decisions or perform repetitive tasks based on certain conditions. | |
8 | + | ||
9 | + | #### What are If Statements? | |
10 | + | ||
11 | + | An `if` statement is used to execute code only when a specified condition is true. It’s one of the basic building blocks of any programming language because it allows for decision-making logic. | |
12 | + | ||
13 | + | **Flow Control Basics:** | |
14 | + | ||
15 | + | - **Condition**: The first part you write in an `if` statement checks if something is true or false. | |
16 | + | - **Action**: Based on whether the condition is met, either execute a block of code (the action) directly after the `if`, or continue to other statements (**else**, `elif`, etc.). | |
17 | + | ||
18 | + | #### Flow Control with Loops | |
19 | + | ||
20 | + | Loops are used when you need to repeat a specific set of instructions multiple times. Python has two types: **for loops** and **while loops**. | |
21 | + | ||
22 | + | 1. **For Loop**: This is used when the number of iterations is known in advance (e.g., "loop 5 times"). | |
23 | + | ||
24 | + | - Syntax: | |
25 | + | ``` | |
26 | + | for variable_name in iterable: | |
27 | + | execute code | |
28 | + | ``` | |
29 | + | ||
30 | + | 2. **While Loop**: This runs as long as a specified condition is true. | |
31 | + | ||
32 | + | - Syntax: | |
33 | + | ``` | |
34 | + | while condition: | |
35 | + | execute code | |
36 | + | ``` | |
37 | + | ||
38 | + | Both loops are flow control structures because they allow you to repeat actions until certain conditions are met or not. | |
39 | + | ||
40 | + | --- | |
41 | + | ||
42 | + | ### Example of Flow Control in Python | |
43 | + | ||
44 | + | Here’s an example that demonstrates both `if` statements and loops: | |
45 | + | ||
46 | + | **Scenario:** You have a list of students with their names, grades, and scores. You want to find the student with the highest grade. | |
47 | + | ||
48 | + | ```python | |
49 | + | students = [ | |
50 | + | {'name': 'John', 'grade': 85}, | |
51 | + | {'name': 'Jane', 'grade': 90}, | |
52 | + | {'name': 'Mike', 'grade': 85} | |
53 | + | ] | |
54 | + | ``` | |
55 | + | ||
56 | + | **Your Program:** | |
57 | + | ||
58 | + | 1. **Loop through each student to find the highest grade and their name.** | |
59 | + | 2. If you need to do this programmatically, use a loop. | |
60 | + | ||
61 | + | ```python | |
62 | + | max_grade = -float('inf') | |
63 | + | highest_name = '' | |
64 | + | ||
65 | + | for student in students: | |
66 | + | if student['grade'] > max_grade: | |
67 | + | max_grade = student['grade'] | |
68 | + | highest_name = student['name'] | |
69 | + | ||
70 | + | # Using an if-else statement to check the maximum grade. | |
71 | + | if max_grade == 90: | |
72 | + | print("Jane has the highest grade.") | |
73 | + | elif max_grade == 85: | |
74 | + | print("Both John and Mike have the same highest grade, which is 85.") | |
75 | + | else: | |
76 | + | print(f"{highest_name} scored {max_grade}.") | |
77 | + | ``` | |
78 | + | ||
79 | + | --- | |
80 | + | ||
81 | + | ### Why These Concepts Are Important | |
82 | + | ||
83 | + | - **Decision Making:** `if` statements allow you to make decisions in your code. | |
84 | + | - **Repetition:** Loops enable you to repeat tasks until a condition is met, which is useful for automating repetitive actions. | |
85 | + | ||
86 | + | By mastering these control structures, you can write more complex and efficient programs. If you have any specific questions or need further clarification on this topic, feel free to ask! |
Novější
Starší