IACS Computes! High School summer camp
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
Day 9
Yesterday we introduced to you the following basics:
int
, float
, str
, bool
and
, or
, not
if
, elif
, else
# int
print(type(1))
# float
print(type(1.0))
# string
print(type("a"))
# boolean
print(type(True))
# complex
print(type(1 + 2j))
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'complex'>
a = 21
b = 10
# Add
print(a + b)
# Subtract
print(a - b)
# Multiplication
print(a * b)
# Divide
print(a / b)
# Modulus
print(a % b)
# Floor division
print(a // b)
# Exponent
a = 2
b = 3
print(a ** b)
print(a ** (0.5))
31
11
210
2.1
1
2
8
1.4142135623730951
True and False
False
True or False
True
not True
False
1 == 2
False
1 > 2
False
2 < 1
False
Condition 1 | Condition 2 | Output |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Condition 1 | Condition 2 | Output |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Example 1: If we want to identify if a given number is odd or even then the control flow is as follows:
n = int(input())
if n % 2 == 0:
print("n is even")
else:
print("n is odd")
7
n is odd
Example 2: Identify if a given number if positive, negative or 0
n = float(input())
if n > 0:
print("n is positive")
elif n < 0:
print("n is negative")
else:
print("n is 0")
7
n is positive
The syntax of a while loop is as follows:
while <condition> :
statements
The while loop will run as long as the <condition>
is satisfied, therefore it is a good practice to make sure the condition is not satisfied at some point in finite time. If the breaking condition is not met then the code will never end (at least in theory. In practice your computer will probably give up and crash!)
Example : Print the first 10 even numbers
i = 1
while i <= 10:
print(2 * i)
i += 1
2
4
6
8
10
12
14
16
18
20