Arithmetic, relational and logical operators, and string handling (length, position, substrings, case) in Python.
| Operator | Meaning / example |
|---|---|
| + - * / | |
| % (modulus) | |
| // (integer division) | |
| ** (exponentiation) | |
| Order of operations | |
| Exam wording |
| Operator | Meaning |
|---|---|
| == and != | |
| < and > | |
| <= and >= | |
| Result | |
| Example | |
| Common slip |
| Operator | Meaning / example |
|---|---|
| and | |
| or | |
| not | |
| Combined example | |
| Truth |
| Operation | Meaning / example |
|---|---|
| len(s) | |
| s[i] | |
| s[a:b] | |
| s.upper() and s.lower() | |
| s.find(x) | |
| Concatenation (+) |
name = "Ander"
print(len(name)) # 5
print(name[0:3]) # And
print(name.upper()) # ANDER
print(name[0]) # A
print(name.find("d")) # 2
if name[0].upper() == "A":
print("Name starts with A")
greeting = "Hello, " + name + "!"
print(greeting) # Hello, Ander!print(10 % 3) # 1
print(8 % 2) # 0, so 8 is even
print(17 // 5) # 3
print(17 % 5) # 2, because 17 = 3 * 5 + 2
print(2 ** 3) # 8
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")What is the result of 17 % 5 in Python?
What does len("Computing") return?
A variable word holds "Python". Write Python to (a) print the first 3 characters, (b) print the word in capitals, (c) print the last character. (3 marks)
Write a Python program that asks the user for a whole number, then prints "FizzBuzz" if it is divisible by both 3 and 5, "Fizz" if divisible by 3 only, "Buzz" if divisible by 5 only, and otherwise prints the number. (4 marks)
What is the result of (5 > 3) and (10 < 8)?
Which Python operator tests that two values are NOT equal?
State what is printed by this code. (2 marks)
word = "computer"
print(word.find("e"))
print(word[2:5])