Python basic syntax (1)

1. Usage of print function (printing)

1.1 Printout

# Print string (Chinese, English, numbers)
print('Hello,Word')
# Print numbers
print(123)

1.2 Three kinds of quotation marks for print

# Single quotes print string text
print('apostrophe')
# Double quotes can print text with single quotes in a string
print("Double quotes")
# Triple quotation marks can print out newline text
print('''Triple quotes''')

2. python wrap#

# You can use triple quotation marks to wrap, output the newline in the text as it is, not automatically wrap
print('''Triple quotation mark 1
Triple quotation mark 2
Triple quotation mark 3''')
# Escape character\n, can realize automatic line wrapping
print('Hello \n Word!')

3. Compiler installation#

3.1 Build a compilation environment
Python download: https://www.python.org/downloads/

3.2 Install editor
Python's own editor: IDLE
Third-party editors: vscode, pycharm

4. Variables and assignment#

Variable: It can be a number, it can be any data type, it can be changed, it is not a constant layer; it is not recommended to use BIF in python.
Assignment: Add a data to the variable to increase the readability of the code.

a ='Assign a value to variable a'

Specification of variable names:
1 ) Cannot use Chinese, only English;
2 ) Can only be an underscore of alphanumeric values;
3 ) Cannot start with a number;
4 ) Cannot use BIF in Python (BIF refers to an internal function in Python) as a variable;
5 ) Try to describe the content of variables.

5. type of data#

5.1 String
str() can convert other data types to strings

number ='60'print(str(number))The 60 printed here is a string type

5.2 Integer
int() can convert other data types to integers

number ='60'print(int(number))The 60 printed here is an integer type

5.3 Floating point number (number with decimal point) float()

number ='60'print(float(number))The result printed here is 60.0, the data type is floating point

6. Splicing symbol'+'

a =80 
b =89
c ='abc'print(str(a)+c)print(a + b)

result:
80 abc
169

7. Verify query data type type() function#

The type function cannot be displayed directly, and other functions need to be called to display such as print.

b =89
c ='abc'print(type(b))print(type(c))

result:
< class'int'><class'str'>

8. Conditional sentence if

One-way judgment, two-way judgment, and multi-way judgment can be used, among which two-way judgment and multi-way judgment do not need to add conditional judgment after else, so it needs to be put at the end

8.1 One-way judgment

a =80if a >100:print('a is greater than 100')

8.2 Two-way judgment

a =80if a >100:print('a is greater than 100')else:print('a is less than 100')

8.3 Multidirectional judgment

a =55if a >90:print('Great God')
elif a >60:print('Pass')else:print('不Pass')

9. One equal sign and two equal signs#

a = 1 assignment (variable assignment)
a == 1 is equal to (compare, judge)

For the above two, an equal sign is used to assign a value to a variable, and two equal signs are used for comparison and judgment

10. if nesting#

If condition is judged, there must be four spaces between the child condition judgement and the parent condition judgement if.

if parent condition:
 if sub-condition 1:
elif sub-condition 2:else:
elif mother condition 2:if sub-condition 1:
elif sub-condition 2:else:else:if sub-condition 1:
elif sub-condition 2:else:

Recommended Posts

Python basic syntax (1)
Python3 basic syntax
Python basic syntax generator
Basic syntax of Python
Python basic syntax iteration
Python basic syntax list production
Python basic summary
Python basic operators
Python basic syntax and number types
Python basic drawing tutorial (1)
Python function basic learning
Basics of Python syntax
python_ crawler basic learning
Python basic data types
Basic knowledge of Python (1)
Python basic data types
Python basic knowledge question bank
Python basic drawing tutorial (two)
Python introductory notes [basic grammar (on)]
Python entry notes [basic grammar (below)]
Python file operation basic process analysis
Python crawler basic knowledge points finishing
Real zero basic Python development web
Basic analysis of Python turtle library implementation
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python basic actual combat-guess the age game
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4