IACS Computes! 2019

IACS Computes! High School summer camp

Binder

Day 1

Day 2

Day 3

Day 4

Day 5

Day 6

Day 7

Day 8

Day 9

View the Project on GitHub harpolea/IACS_computes_2019

Review

Yesterday we introduced to you the following basics:

  1. Data types : int, float, str, bool
  2. Arithmetic Operators : +, -, , /, //, %, **
  3. Logical Operators : and, or, not
  4. Conditionals : if, elif, else
  5. While loops

1. Data Types

# 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'>

2. Arithmetics

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

3. Logical Operators

True and False
False
True or False
True
not True
False
1 == 2
False
1 > 2
False
2 < 1
False

And Operator

Condition 1 Condition 2 Output
True True True
True False False
False True False
False False False

Or Operator

Condition 1 Condition 2 Output
True True True
True False True
False True True
False False False

4. Conditionals

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

5. While Loop

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

Back to day 2