- course Info
- what is computation
- python basics
- mathematical operations
- python variables and types
Burkay Genç, Ahmet Selman Bozkır, and Selma Dilek
20/02/2024
declarative knowledge is statements of fact.
imperative knowledge is a recipe or “how-to”.
square root of a number is such that
recipe for deducing square root of a number
3 | 9 | 16/3 | 4.17 |
4.17 | 17.36 | 3.837 | 4.0035 |
4.0035 | 16.0277 | 3.997 | 4.000002 |
1 + 2 + 3 = an algorithm!
English:
“cat dog boy” | not syntactically valid |
“cat hugs boy” | syntactically valid |
programming language:
“hi”5 | not syntactically valid |
3.2 * 5 | syntactically valid |
English:
“I are hungry” | syntactically valid but static semantic error |
programming language:
3.2*5 | syntactically valid |
3+“hi” | static semantic error |
"Flying planes can be dangerous"
x = 5 print(x * 2)
## 10
x = 5 print(x ** 2)
## 25
print(3.a)
SyntaxError: invalid syntax
a = 5 3a + 2
SyntaxError: invalid syntax
a - 3'a'
SyntaxError: invalid syntax
3 + 'a'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
int
– represent integers, ex. 5
float
– represent real numbers, ex. 3.27
bool
– represent Boolean values True
and False
NoneType
– special and has one value, None
type()
to see the type of an objecttype(5)
## <class 'int'>
type(3.0)
## <class 'float'>
name = "burkay" print(name[2:4])
## rk
float(3)
converts integer 3
to float 3.0
int(3.9)
truncates float 3.9
to integer 3
float(3)
## 3.0
int(3.9)
## 3
int("burkay")
## ValueError: invalid literal for int() with base 10: 'burkay'
print
commandprint("Hello World!")
## Hello World!
print(3 + 2)
## 5
print("My age is", 41)
## My age is 41
<object> <operator> <object>
i+j
the sum
i-j
the difference
i*j
the product
i/j
the division
For the sum, the difference and the product, if both objects are integers then the result is an integer. If one or both are floats, then the result is a float.
For the division the result is always a float.
i%j
the remainder when i
is divided by j
i**j
i
to the power of j
3 + 5
## 8
3 - 5
## -2
3 * 5
## 15
3 / 5
## 0.6
32 % 5
## 2
3 ** 4
## 81
3 * (2 + 5)
## 21
**
*
/
+
and –
executed left to right, as appear in expression3 * 2 + 5
## 11
# variable = value pi = 3.14159 pi_approx = 22/7
pi
pi
## 3.14159
pi_approx
## 3.142857142857143
a, b = 3, 5 a
## 3
b
## 5
a, b, c = 5, 8, 3.14 d = c * a ** 2 * b
vs.
r, h, pi = 5, 8, 3.14 V_cyl = pi * (r ** 2) * h
#
#
are ignored by Python# Radius, height and pi are defined r, h, pi = 5, 8, 3.14 # The volume of the cylinder is computed # Volume is equal to height times the base area V_cyl = pi * (r ** 2) * h
pi = 3.14159 radius = 2.2 area = pi * (radius ** 2) print(area)
## 15.205295600000001
pi = 3.14159 radius = 2.2 # area of circle area = pi * (radius**2) print(area)
## 15.205295600000001
radius = radius + 1 print(radius)
## 3.2
print(area)
## 15.205295600000001
area = pi * (radius**2) print(area)
## 32.169881600000004
pi = 3.14 radius = 2.2 area = pi * (radius**2) radius = radius + 1
type(5) print(3.0 - 1)
Try the code: https://www.tutorialspoint.com/python/online-python-compiler.php
a = 6 b = 7 c = 1 total = a + b + c print(total) c += 1 # Same as: c = c + 1 print(total)
These slides are a direct adaptation of the slides used for MIT 6.0001 course present (as of February 2020) on MIT OCW web site.
Original work by:
Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts Institute of Technology: MIT OpenCourseWare. License: Creative Commons BY-NC-SA.
Adapted by and for:
Assoc. Prof. Dr. Burkay Genç. MUH101 Introduction to Programming, Fall 2022 Hacettepe University, Computer Engineering Department.