Comment
1 | ''' I am a note I am a note ''' |
type of data
- Integer type: 10011101
- String type: "10,011,101"
- List type: [10, 011, 101]
Precautions
- single and double quotes are used exactly the same in python.
- Use triple quotes (''' or """) to specify a multi-line string.
- escape character is '\'
- backslashes can be used to escape, use r to make backslashes not escape: r"\n"
- Strings can be joined together with the + operator and repeated with the * operator.
read variables
1 | a = input("Enter the value of a") |
Output function
output variable
1 | print(a) |
output character
1 | print("hello, world!") |
The default output of print is newline, if you want to achieve no newline, you need to add at the end of the variable
end=""
E.g
1 | print('This is a string,', end="") print('The string here will not start a new line') |
output
This is the string, the string here will not start on a new line
Evaluation function
basic grammar
1 | eval(expression[, globals[, locals]]) |
parameter:
- expression: Expression.
- globals: variable scope, global namespace, if provided, must be a dictionary object.
- locals: variable scope, local namespace, if provided, can be any mapping object.
Effect: A function that removes the outermost quotation marks of the parameters and executes the rest of the statement, which can be used as a string to integer or floating point number
example:
1 | >>> x = 7 >>> eval('3*x') twenty one >>> eval('pow(2,2)') 4 >>> eval('2 + 2') 4 >>> n=81 >>> eval("n + 4") 85 |
Multi-line statement
Python usually writes a statement on one line, but if the statement is very long, we can use the backslash () to implement a multi-line statement, for example:
1 | total = item_one + \ item_two + \ item_three |
Multi-line statements within [], {}, or () do not require a backslash (), for example:
1 | total = ['item_one', 'item_two', 'item_three','item_four', 'item_five'] |
loop statement
Take while as an example:
1 | while Judging the condition (condition): Execute statements (statements)... break #break out of the loop |
GIF demo:
Generate random numbers
example:
1 | import random #Import random module random.randint(1,10) #Generate random integers from 1 to 10 |
Thanks
Reprinted from:
Zeruns's Blog
Python theoretical knowledge(2)basic statement
Comments