본문 바로가기
Study Note/Python

3. IF & loop

by jhleeatl 2024. 3. 25.

IF is one of the most commonly used functions as a conditional statement. Here, IF, Else, and spacing are very important.

 

Example

money = 3000
if money > 3800:
    print ('Take taxi!')
else :
    print ('Can not take taxi')
    print ('what to take?')

Result

 

money = 5000
if money > 3800:
    print ('Take taxi!')
else :
    print ('Can not take taxi')
    print ('what to take?')

Result

 

 

If there are not space

money = 5000
if money > 3800:
    print ('Take taxi!')
else :
    print ('Can not take taxi')
print ('what to take?')

 

reult

 

 

If there is no space after 'Else', it may be recognized as a different block, causing it not to be included in the conditional statement.

 

Elif

When adding another condition, 'elif' is used.

money = 2000
if money > 3800:
    print ('Take taxi!')
elif money>1200:
    print('take bus')
else :
    print ('Can not take taxi')

 

Result : 'take bus'

 

 

 

Loop example

people = [
    {'name': 'bob', 'age': 20},
    {'name': 'carry', 'age': 38},
    {'name': 'john', 'age': 7},
    {'name': 'smith', 'age': 17},
    {'name': 'ben', 'age': 27},
    {'name': 'bobby', 'age': 57},
    {'name': 'red', 'age': 32},
    {'name': 'queen', 'age': 25}
]
for person in people:
    name = person['name']
    age = person['age']
    if age>20:
        print (name, age)

 

Note 

 

I wondered what's the purpose of using 'for' and 'in'

 The reason for using "for in" is to iterate over each element in the list (or iterable) to perform a repetitive task. For example, in the given list 'people', we use the 'for in' loop to iterate over each dictionary containing person information. We assign each dictionary to the variable 'person', extract the name and age from each dictionary, and then print the name and age of individuals who are older than 20. This loop allows us to perform a series of tasks repeatedly for each element in the list.

 

What's the difference between : and =

 

The difference between "=" and ":" in the given code is as follows:

1. *= (Assignment Operator)*:
   - '=' is used to assign a value to a variable.
   - It is used for storing or modifying values in variables.
   
   Example:

   name = person['name']
   age = person['age']


2. *: (Colon)*:
   - ':' is typically used after headers of conditional statements (if, elif, else) or loops (for, while).
   - It indicates the beginning of the code block associated with the conditional or loop.
   - It marks the start of a code block, and the indentation defines the scope of the block.
   
   Example:

  for person in people:


In summary, '=' is used for assigning values to variables, while ':' is used to denote the beginning of code blocks for conditional statements or loops, defining their scope through indentation.

 

 

Enumerate & Break

people = [
    {'name': 'bob', 'age': 20},
    {'name': 'carry', 'age': 38},
    {'name': 'john', 'age': 7},
    {'name': 'smith', 'age': 17},
    {'name': 'ben', 'age': 27},
    {'name': 'bobby', 'age': 57},
    {'name': 'red', 'age': 32},
    {'name': 'queen', 'age': 25}
]
for i, person in enumerate(people):
    name = person['name']
    age = person['age']
    print (i, name, age)
    if i>3:
        break

 

 

Using "enumerate" and "break" allows for only printing up to "4 ben 27". This function is useful when rendering code with a large number of raws, as it can take a long time. Whenever you are finding debug in the large number of raws, you can use "enumerate" and "break" to reduce the time.

'Study Note > Python' 카테고리의 다른 글

6. Set & tuples and list  (0) 2024.03.27
5. Uses of [] {} ()  (0) 2024.03.26
4. Function [Def]  (0) 2024.03.26
2. List & dictionary  (0) 2024.03.25
1. Variable declaration and data types  (0) 2024.03.25