Common operations
Input and output
Values are input using the INPUT command as follows:
INPUT <identifier>The identifier should be a variable (that may be an individual element of a data structure such as an array). Values are output using the OUTPUT command as follows:
OUTPUT <value(s)>Several values, separated by commas, can be output using the same command.
Example – INPUT and OUTPUT statements
INPUT Answer
OUTPUT Score
OUTPUT "You have ", Lives, " lives left"Python's input() method defaults to the string str, so if you input the number 1, the program will get the string '1', not the mathematical 1.
print('1'+'1')
print(1+1)You can see that the addition of the string 1 is the concatenation of the two to get '11', and the addition of the number 1 can get the result of the operation 2.
So in the input(), if you want to use the input value as a number, you must do a type conversion.
Convert type
Method | Example |
|---|---|
int | int('4') #Convert '4' to 4. |
float | float('4.5') #Convert '4.5' to 4.5. |
str | str(4) #Convert 4 to '4'. |
string = input()
num = int(input())
real = float(input())
print(string+string)
print(num+num)
print(real+real)Output with space
Python can output multiple elements at the same time, with space by default.
print(1,2,3) # Output 1 2 3Arithmetic operations
Standard arithmetic operator symbols are used:
Symbol | Explanation |
|---|---|
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
^ | raised to the power of |
Example – arithmetic operations
Answer ← Score * 100 / MaxMark
Answer ← Pi * Radius ^ 2The integer division operators MOD and DIV can also be used.
DIV(<identifier1>, <identifier2>)Returns the quotient of identifier1 divided by identifier2 with the fractional part discarded.
MOD(<identifier1>, <identifier2>)Returns the remainder of identifier1 divided by identifier2 The identifiers are of data type integer.
Example – MOD and DIV
DIV(10, 3) returns 3
MOD(10, 3) returns 1Multiplication and division have higher precedence over addition and subtraction (this is the normal mathematical convention). However, it is good practice to make the order of operations in complex expressions explicit by using parentheses.
Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 2 => 7 |
- | Subtraction | 5 – 2 => 3 |
* | Multiply | 5 * 2 => 10 |
/ | Divide | 5 / 2 => 2.5 |
% | Mod | 5 % 2 => 1 |
** | Power | 5 ** 2 => 25 |
// | Whole Divide | 5 // 2 => 2 |
a = 5
b = 2
print(a+b) #7
print(a-b) #3
print(a*b) #10
print(a/b) #2.5
print(a%b) #1
print(a**b) #25
print(a//b) #2Logical operators
The following symbols are used for logical operators:
Symbol | Explanation |
|---|---|
= | equal to |
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
<> | not equal to |
The result of these operations is always of data type BOOLEAN.
In complex expressions, it is advisable to use parentheses to make the order of operations explicit.
In Python, an equal sign = is an assignment, assigning the value on the right to the variable on the left, and two equal signs == are used to determine whether they are equal, and are an operator that gives the result True or False.
a = 5
b = 2
print(a==b) #False
print(a!=b) #True
print(a>b) #True
print(a<b) #False
print(a>=b) #True
print(a<=b) #FalseBoolean operators
The only Boolean operators used are AND, OR and NOT. The operands and results of these operations are always of data type BOOLEAN.
In complex expressions, it is advisable to use parentheses to make the order of operations explicit.
Example – Boolean operations
IF Answer < 0 OR Answer > 100
THEN
Correct ← FALSE ELSE
Correct ← TRUE ENDIFa = 5
b = 3
c = 8
print(a > b and a > c) #False
print(a > b or a > c) #True
print(not a > c) #TrueString operations
LENGTH(<identifier>)
Returns the integer value representing the length of string. The identifier should be of data type string.
LCASE(<identifier>)
Returns the string/character with all characters in lower case. The identifier should be of data type string or char.
UCASE(<identifier>)
Returns the string/character with all characters in upper case. The identifier should be of data type string or char.
SUBSTRING(<identifier>, <start>, <length>)
Returns a string of length length starting at position start. The identifier should be of data type string, length and start should be positive and data type integer.
Generally, a start position of 1 is the first character in the string.
Example – string operations
LENGTH("Happy Days") //will return 10
LCASE(ꞌWꞌ) //will return ꞌwꞌ
UCASE("Happy") //will return "HAPPY"
SUBSTRING("Happy Days", 1, 5) //will return "Happy"Other library routines
ROUND(<identifier>, <places>)
Returns the value of the identifier rounded to places number of decimal places. The identifier should be of data type real, places should be data type integer.
RANDOM()
Returns a random number between 0 and 1 inclusive.
Example – ROUND and RANDOM
Value ← ROUND (RANDOM() * 6, 0) // returns a whole number between 0 and 6LENGTH("Happy Days")
a = len("Happy Days") # 10LCASE("Happy")
a = "Happy".lower() # HAPPYUCASE("Happy")
a = "Happy".upper() # HAPPYSUBSTRING("Happy Days", 1, 5)
a = "Happy Days"[0:5] # HAPPYROUND(1.234,1)
a = round(1.234,1) # 1.2RANDOM()
import random
a = random.random() #0~1