# Basic Operators
# Assignment Operator
a = 1
b = "hello"
2
We can assignment multiple variables at the same time
a, b = 1, "hello"
# Arithmetic operators
Basic arithmetic operations include:
- addition
- subtraction
- multiplication
- division
- modulus
- exponentiation
- floor division
print(2 + 3)
print(2 - 3)
print(2 * 3)
print(2 / 3)
print(2 % 3)
print(2 ** 3)
print(2 // 3)
print(3 // 2)
2
3
4
5
6
7
8
A shortcut
We can chain the assignment operator with arithmetic operators
value = 100
value = value + 1
2
is equivalent to
value = 100
value += 1
2
# Assignment 2
Create a Python script named circle.py
that prompts for the diameter (cm) of a circle and computes and displays the circle’s area and circumference.
You can assume π equals 3.14159.
Sample Run of the script looks like the following
python circle.py
Enter Diameter: 10
Area = 78.53975 cm²
Circumference = 31.4159 cm
2
3
4
Converting data types
Since input()
always returns a string, we need to cast the return value into a number.
To convert a string into an integer, we can
int('123')
Similarly we can convert a string into a decimal number
float('456')
Or we can convert a number into a string
str(123.456)
Sample Solution
diameter = float(input("Enter Diameter: "))
pi = 3.14159
print("Area = " + str(pi * (diameter / 2) ** 2) + " cm²")
print("Circumference =", pi * diameter, "cm")
2
3
4
5
# Comparison operators
Say, after setting up x
and y
, the comparisons are as follow:
x, y = 1, 2
print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)
2
3
4
5
6
7
8
A comparison operator always returns a boolean value (True
/ False
).
# Logical Operators
We use logical operators to combine booleans (conditions).
For example, after setting up v
v = 10
print(v > 2 and v < 8)
print(v > 2 or v < 8)
print(not v > 2)
print(not v < 8)
2
3
4
5
6
and
returnsTrue
if both booleans (conditions) areTrue
or
returnsTrue
if at least one of booleans (conditions) isTrue
not
negates the boolean (condition), returningTrue
if givenFalse
, and vice versa
# Bitwise operators
To understand bitwise operations, we need to learn about binary numbers.
We use base ten numerals daily. When we see a decimal number 654,321
, it represents
Hundreds Thousands | Tens Thousands | Thousands | Hundreds | Tens | Ones |
---|---|---|---|---|---|
6 | 5 | 4 | 3 | 2 | 1 |
Or
105 | 104 | 103 | 102 | 101 | 100 |
---|---|---|---|---|---|
6 | 5 | 4 | 3 | 2 | 1 |
Similarly, binary numbers is base two numerals. When we see a binary number 101010
, it represents
25 | 24 | 23 | 22 | 21 | 20 |
---|---|---|---|---|---|
1 | 0 | 1 | 0 | 1 | 0 |
Therefore, the binary number 101010
has a decimal value of 42
.
Each column is considered a bit. When we perform bitwise operations, we do it on each matching bit of two binary numbers.
The truth table of and
, or
, and exclusive or (xor)
is as follow:
input | output | |||
---|---|---|---|---|
x | y | & (and) | | (or) | ^ (xor) |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
There is also negation.
input | output |
---|---|
x | ~ (not) |
0 | 1 |
1 | 0 |
TIP
These basic bitwise operations have corresponding hardware implementations (logic gates (opens new window)). An integrated circuit consists of many logic gates and other physical components.
print(4 << 5)
print(4 >> 2)
print(4 & 5)
print(4 | 5)
print(4 ^ 5)
print(~1)
2
3
4
5
6
WARNING
Applying ~
to a number in Python requires knowledge about Two's complement (opens new window).
It also has to deal with sign (positive/negative). We may talk about this in future.
# Assignment 2.5
Kattis Problem Archive (opens new window) is a great source to practice programming and problem solving skills. Let's take a look at problem R2 (opens new window).
Given that you can use the following code to capture user input of r1
and s
, how would you solve this problem?
r1, s = input().split()