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, or a custom data type).
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":: details Python
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 3::
Arithmetic operations
Standard arithmetic operator symbols are used:
+ Addition
- Subtraction
* Multiplication
/ Division (The resulting value should be of data type REAL, even if the operands are integers.)
DIV Integer division: Used to find the quotient (integer number before the decimal point) after division.
MOD or Modulus: The remainder that is left over when one number is divided by another.
Multiplication 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.
运算符 | 描述 | 示例 |
|---|---|---|
+ | 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) #2Relational operations
The following symbols are used for relational operators (also known as comparison operators):
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= 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) #FalseLogic operators
The only logic operators (also called relational 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.
a = 5
b = 3
c = 8
print(a > b and a > c) #False
print(a > b or a > c) #True
print(not a > c) #TrueString functions and operations
The AS & A Level (9618) syllabus specifically requires candidates to know string manipulation
functions in their chosen programming language. Pseudocode string manipulation functions will always be provided in examinations. Some basic string manipulation functions are given here.
Each function returns an error if the function call is not properly formed.
LENGTH(ThisString : STRING) RETURNS INTEGER
//returns the integer value representing the length of ThisString
//Example: LENGTH("Happy Days") returns 10LEFT(ThisString : STRING, x : INTEGER) RETURNS STRING
//returns rightmost x characters from ThisString
//Example: LEFT("ABCDEFGH", 3) returns "ABC"RIGHT(ThisString : STRING, x : INTEGER) RETURNS STRING
//returns rightmost x characters from ThisString
//Example: RIGHT("ABCDEFGH", 3) returns "FGH"MID(ThisString : STRING, x : INTEGER, y : INTEGER) RETURNS STRING
//returns a string of length y starting at position x from ThisString
//Example: MID("ABCDEFGH", 2, 3) returns "BCD"LCASE(ThisChar : CHAR) RETURNS CHAR
//returns the character value representing the lower-case equivalent of ThisChar If ThisChar is not an upper-case alphabetic character, it is returned unchanged.
//Example: LCASE('W') returns 'w'UCASE(ThisChar : CHAR) RETURNS CHAR
//returns the character value representing the upper-case equivalent of ThisChar If ThisChar is not a lower-case alphabetic character, it is returned unchanged.
//Example: UCASE('h') returns 'H'ASC(ThisChar : CHAR) RETURNS INTEGER
//returns the ASCII code value of this character
//Example: ASC('A') returns 65CHR(ThisNumber : INTEGER) RETURNS CHAR
//returns the ASCII character of this value
//Example: CHR(65) returns 'A'STR_TO_NUM(Str : STRING) RETURNS INTEGER/REAL
//returns the integer or real value of this string
//Example: STR_TO_NUM("12") returns 12NUM_TO_STR(Num : INTEGER/REAL) RETURNS STRING
//returns the string value of this number
//Example: NUM_TO_STR(12) returns "12"TO_UPPER(Str : STRING) RETURNS STRING
//returns the upper value of this string
//Example: TO_UPPER("apple") returns "APPLE"TO_LOWER(Str : STRING) RETURNS STRING
//returns the lower value of this string
//Example: TO_LOWER("APPLE") returns "apple"IS_NUM(Str : STRING/CHAR) RETURNS BOOLEAN
//returns TRUE if this string or char is a number
//Example: IS_NUM("27") returns TRUEIn pseudocode, the operator & is used to concatenate (join) two strings.
Example: "Summer" & " " & "Pudding" produces "Summer Pudding"
Where string operations (such as concatenation, searching and splitting) are used in a programming
language, these should be explained clearly, as they vary considerably between systems.
Where functions in programming languages are used to format numbers as strings for output, their use should also be explained.
Numeric functions
INT(x : REAL) RETURNS INTEGER
//returns the integer part of x
//Example: INT(27.5415) returns 27RAND(x : INTEGER) RETURNS REAL
//returns a random real number in the range 0 to x (not inclusive of x)
//Example: RAND(87) may return 35.43RIGHT("Happy Days",4)
a = "Happy Days"[-4:] # DaysLENGTH("Happy Days")
a = len("Happy Days") # 10MID("Happy Days",1,5)
a = len("Happy Days")[0:5] # HappyLCASE("Happy")
a = "Happy".lower() # HAPPYUCASE("Happy")
a = "Happy".upper() # HAPPYINT(1.2)
a = round(1.2,1) # 1RAND(10)
import random
a = random.random()*10 #0~10