Python theoretical knowledge(2)basic statement

Comment

Python
1
2
3
4
'''
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

Python
1
a = input("Enter the value of a")

Output function

output variable

Python
1
print(a)

output character

Python
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

Python
1
2
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

Python
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:

Python
1
2
3
4
5
6
7
8
9
10
>>> 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:

Python
1
2
3
total = item_one + \
        item_two + \
        item_three

Multi-line statements within [], {}, or () do not require a backslash (), for example:

Python
1
2
total = ['item_one', 'item_two',
'item_three','item_four', 'item_five']

loop statement

Take while as an example:

Python
1
2
3
while Judging the condition (condition):
    Execute statements (statements)...
break #break out of the loop

GIF demo:

Generate random numbers

example:

Python
1
2
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

https://blog.tsinbei.com/en/archives/553/

Author
Hsukqi Lee
Posted on

2022-07-05

Edited on

2022-07-28

Licensed under

CC BY-NC-ND 4.0

Comments

Name
Mail
Site
None yet