-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path4
More file actions
210 lines (160 loc) · 5.71 KB
/
4
File metadata and controls
210 lines (160 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Day-4 | User Inputs, Control Statements, Loops
## User Inputs
To take user inputs in Python, you can use the `input()` function. By default, `input()` returns a string, so you'll need to convert it to the desired data type (int, float, or string) using `int()`, `float()`, or leave it as is.
Here's an example code that takes user inputs in different data types:
```python
# Taking an integer input
user_input_int = int(input("Enter an integer: "))
# Taking a floating-point input
user_input_float = float(input("Enter a float: "))
# Taking a string input
user_input_string = input("Enter a string: ")
# Printing the inputs
print(f"You entered an integer: {user_input_int}")
print(f"You entered a float: {user_input_float}")
print(f"You entered a string: {user_input_string}")
```
In this example, `int(input(...))` is used to take an integer input, `float(input(...))` is used for a floating-point input, and `input(...)` alone is used for a string input.
Keep in mind that if the user provides input that cannot be converted to the specified data type, a `ValueError` will be raised. You might want to handle this using try-except blocks if necessary.
## Control Statements
Certainly! Here are examples of `if`, `else`, `elif`, and nested `if` statements in Python:
### `if` statement:
```python
# Example 1: Checking if a number is positive
num = 10
if num > 0:
print("The number is positive.")
```
### `if-else` statement:
```python
# Example 2: Checking if a number is even or odd
num = 7
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
```
### `elif` statement:
```python
# Example 3: Determining the grade based on a score
score = 75
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
elif score >= 70:
print("Grade C")
elif score >= 60:
print("Grade D")
else:
print("Grade F")
```
### Nested `if` statements:
```python
# Example 4: Nested if statements to determine eligibility for a loan
income = 50000
credit_score = 700
if income >= 60000:
if credit_score >= 700:
print("You are eligible for a loan with a low interest rate.")
else:
print("You are eligible for a loan with a higher interest rate.")
else:
print("You are not eligible for a loan.")
```
In the last example, we have a nested `if` statement. This means there is an `if` statement inside another `if` statement. The inner `if` statement is only executed if the condition of the outer `if` statement is true.
Remember, in Python, indentation is crucial. The code within a block is indented, and the standard convention is to use 4 spaces for each level of indentation.
These examples showcase the basic usage of conditional statements in Python. Depending on the condition(s) evaluated, different code blocks are executed.
## Loops
### 1. **For Loops:**
- Use `for` loops to iterate over a sequence (e.g., a list, tuple, or string).
```python
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
```
### 2. **While Loops:**
- Use `while` loops to repeatedly execute a block of code while a condition is `True`.
```python
count = 0
while count < 5:
print(count)
count += 1
```
### 3. **Break and Continue:**
- Use `break` to exit a loop prematurely, and `continue` to skip the current iteration.
```python
for i in range(10):
if i == 5:
break
print(i)
for i in range(10):
if i == 5:
continue
print(i)
```
### 4. **Looping through Dictionaries:**
- You can loop through the keys, values, or items of a dictionary.
```python
person = {'name': 'John', 'age': 30, 'city': 'New York'}
for key, value in person.items():
print(f'{key}: {value}')
```
### 5. **Loop Control with Else:**
- You can use an `else` block with a loop, which executes when the loop completes without hitting a `break`.
```python
for i in range(5):
print(i)
else:
print("Loop completed without a break.")
```
### 6. **List Comprehensions:**
- A concise way to create lists.
```python
numbers = [i for i in range(10) if i % 2 == 0]
```
These control structures and loops are powerful tools in Python that allow you to build complex programs and automate repetitive tasks. Practice using them in various scenarios to become proficient in controlling the flow of your code. Happy coding! 🚀
## SAMPLE PROGRAM
Certainly! Below is a sample Python program that demonstrates user inputs, control statements (if-else), and loops (for loop and while loop):
```python
# User inputs
name = input("Enter your name: ")
age = int(input("Enter your age: "))
# Control statement (if-else)
if age >= 18:
print(f"Hello {name}! You are eligible to vote.")
else:
print(f"Hello {name}! You are not yet eligible to vote. You can vote in {18 - age} years.")
# For loop
print("Printing numbers from 1 to 5 using a for loop:")
for i in range(1, 6):
print(i)
# While loop
num = int(input("Enter a number: "))
fact = 1
i = 1
while i <= num:
fact *= i
i += 1
print(f"The factorial of {num} is: {fact}")
```
This program does the following:
1. Takes user inputs for `name` and `age`.
2. Uses an `if-else` statement to check if the user is eligible to vote based on their age.
3. Prints numbers from 1 to 5 using a `for` loop.
4. Calculates the factorial of a number using a `while` loop.
Here's an example of how the program would work:
```
Enter your name: Alice
Enter your age: 22
Hello Alice! You are eligible to vote.
Printing numbers from 1 to 5 using a for loop:
1
2
3
4
5
Enter a number: 5
The factorial of 5 is: 120
```
This program showcases the use of user inputs, control statements (if-else), and loops (for loop and while loop) in Python.