7/15 21: 00 ~ Held at ZOOM
――By the way, what is Bool type? --while statement --break statement --Chat
Bool types have True
and False
.
Use the type
method to check the type of the object you normally use.
Try: Let's reconfirm what type of object we usually use.
Answer example
Example
print(type(1))
# => <class 'int'>
print(type('hogehoge'))
# => <class 'str'>
print(type(False))
# => <class 'bool'>
print(type(Ture))
# => <class 'bool'>
A conditional expression of an if statement that is used casually. The contents are basically Bool type. In other words, when creating a condition, make sure that the formula is a Bool type.
python
hoge = True
if hoge:
print('The contents of hoge is True')
else:
print('The contents of hoge is False')
# =>The contents of hoge is True
By the way, operators such as ==
, >
and ʻor`, which are often used in conditional expressions, are also returned as Bool type.
python
a = 0
b = 1
print(a == b)
# => False
print(a < b)
# => True
print(a == b or a < b)
# => True
So far, the basics are basic, but just knowing that the conditional expression is a Bool type, it seems that the if statement that I have already studied and the while statement that I will study will be easier to understand, so I added it to the agenda.
As an application, since true and 1, false and 0 are equivalent
python
print(True == 1)
print(False == 0)
Both return True
. Find out why this happens.
The while statement is the basic syntax used for iterative processing.
python
while conditional expression:
Iterative processing when the condition is True
For example, when "output 1 to 10"
python
i = 1 #It is called a loop variable. Prepare to count
while i <= 10:
print(f'The numbers are{i}')
i += 1 #I'm adding one by one. Without this, the condition would be True and an infinite loop.
Try: Let's output 2 to the 0th power to 2 to the 10th power. (Any number to the 0th power is 1)
Answer example
Example
i = 0
while i <=10:
print(2 ** i) # **Find the multiplier with the operator
i += 1
The break statement is used to get out of the iterative process.
python
i = 0
while 1:
print(i)
if i > 2:
break
i += 1
# => 0
# => 1
# => 2
# => 3
Try: Let's create a program that outputs "Please enter characters" until "exit" is entered. By the way, why does
while 1:
make an infinite loop? Let's go back to the Bool type story
Answer example
Example
while 1:
str = input('Please enter the characters>> ')
if str == 'exit':
break
Recommended Posts