Introduction to python (Internship)

Posted by Cong on Sat, 22 Jan 2022 04:11:46 +0100

Write in front

This blog is a non systematic introductory tutorial, which is only for bloggers to sort out the relevant knowledge points of high-frequency operations involved in the use of python.

1 Introduction to Python

python download and installation

Download address: python official website

Click Downloads and select your favorite version

It is recommended to use the executable installer and install the non current latest version, because the latest version is often unstable and some errors are difficult to search and solve.

Test for successful installation

  • windows key (window shape, composed of four squares) + R
  • Enter 'cmd' in the pop-up window
  • Enter python again, and the display is as follows: the installation is successful (finally, enter exit() to exit)

Installation of pycham

Python itself is an interactive operation, similar to one question and one answer. It is very suitable for learning / verifying Python syntax or local code, but it also has the disadvantages that the code can not be saved and too large programs can not be run. Therefore, we need to install Python's integrated development environment - pycharm to facilitate more complex and diverse code writing requirements.

Download address: pycharm official website

Write the first Python program using pychar

  1. Run pychart, select Create New Project, and create a new Python project.
  2. Select 'Pure Python' to create a new Pure Python project. Location indicates the save path of the project. Interpreter is used to specify the version of Python timer.
  3. Right click the project, select New, and then select Python File
  4. Enter the file name mycode in the pop-up dialog box and click OK to create a text file of Python program. The suffix of the text file is default py
  5. In the new mycode Py file, enter the following code, and right-click in the blank space and select Run to Run, indicating that a hello world string is output.
  6. After running successfully, the pycham console window will display our output results. ( ↙)

For ease of use, it is recommended to create a new directory first, and then write and save the small module code in the subdirectory

  • File - Settings - Appearance & Behavior - appearance
  • File - Settings - Editor - Font

    List of commonly used pip download sources in China:
  • Alibaba cloud http://mirrors.aliyun.com/pypi/simple/ China University of science and technology
  • https://pypi.mirrors.ustc.edu.cn/simple/ Douban
  • http://pypi.douban.com/simple/ Tsinghua University
  • https://pypi.tuna.tsinghua.edu.cn/simple/ University of science and technology of China
  • http://pypi.mirrors.ustc.edu.cn/simple/

Some practical shortcuts

  1. ctrl+d quickly copy one line or several lines in the selected area
  2. ctrl+shift+F10 (py.file execution shortcut)

About notes

Single-Line Comments

#Single line comment, do not execute

Multiline comment: start with '' and end with ''

'''
                               _ooOoo_
                              o8888888o
                              88" . "88
                              (| -_- |)
                              O\  =  /O
                           ____/`---'\____
                         .'  \\|     |//  `.
                        /  \\|||  :  |||//  \
                       /  _||||| -:- |||||-  \
                       |   | \\\  -  /// |   |
                       | \_|  ''\---/''  |   |
                       \  .-\__  `-`  ___/-. /
                     ___`. .'  /--.--\  `. . __
                  ."" '<  `.___\_<|>_/___.'  >'"".
                 | | :  `- \`.;`\ _ /`;.`/ - ` : | |
                 \  \ `-.   \_ __\ /__ _/   .-` /  /
            ======`-.____`-.___\_____/___.-`____.-'======
                               `=---='
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       Buddha bless        Never BUG
              Buddha said:
                     Office building, office, office programmer;
                     Programmers write programs and exchange them for wine.
                     Drunk only sit on the Internet, drunk also come to sleep under the Internet;
                     Drunk and sober day after day, online and offline year after year.
                     I wish I would die in the computer room and would not bow before the boss;
                     Mercedes Benz and BMW are expensive and interesting, and buses are self programmers.
                     Others laugh at my madness, and I laugh at my cheap life;
                     If you don't see all the beautiful girls in the street, who belongs to the programmer?
'''

2. Variables and types

Variables and types

# Variable = value; Put the value on the right into the container on the left
bing = 'elephant'

#The two sides are not equal
b1, b2, *b3 = 'a', 'b', 'c', 'd', 'e'
print(b1, b2, b3)

b1, *b2, b3 = 'a', 'b', 'c', 'd', 'e'
print(b1, b2, b3)

Operation results

a b ['c', 'd', 'e']
a ['b', 'c', 'd'] e

Variable has no type, data has type

'''
value type      example
    integer      age = 18
    float     salary = 17.32
 character string        book = 'little prince'
Boolean type      True & False title case
 list          classroom = [21, 7, 'hello', True, [212,323]]
tuple          weather = ('Wuhan', 30)
Dictionaries          content = {'name':'xiaoming', 'height':'172cm', 'hobby':'running'}
'''

Name of variable

Naming rules:

  1. An identifier consists of letters, underscores, and numbers, and cannot begin with a number.
  2. Strictly case sensitive.
  3. Keywords cannot be used. (blue highlight in pycharm)

The concept of keyword is some identifiers with special functions, which is the so-called keyword. Keywords have been officially used by python, so developers are not allowed to define identifiers with the same name as keywords.

Hump nomenclature:

  • lower camel case: the first word starts with a lowercase letter; The first letter of the second word is capitalized, for example: myName, aDog
  • upper camel case: the first letter of each word is capitalized, such as FirstName and LastName

output

print('hello world') #General output
user = 'ZhangSan'
print('hello %s' % user) #Format output
age = 17
print('%s is %d years old' % (user, age))

Operation results:

hello world
hello ZhangSan
ZhangSan is 17 years old

Common format symbols

Wrap output

print('=======My business card=======')
print('full name:%s\nQQ: %d\n cell-phone number: %d\n Company address: %s' % (name, QQ, tel, address))
print('======================')

Result output:

=======My business card=======
full name: chris
QQ: 2736368065
 cell-phone number: 159654538769
 Company address: Wuhan
======================

Simplified border writing method (the same effect as above):

print('='*7 + 'My business card' + '='*7)
print('full name:%s\nQQ: %d\n cell-phone number: %d\n Company address: %s' % (name, QQ, tel, address))
print('='*20)

input

password = input("Please input a password:") #Receive user input
print('The password you just entered is:%s' % password)

Output results:

Note that no matter what type of data we enter here, the final result is a string (% s)

print(type(password))
<class 'str'>

Type conversion

Common type conversion

example:

num1 = '545'
print(type(num1))

num2 = 345
print(type(num2))

# print(num1 + num2)   TypeError: can only concatenate str (not "int") to str
print(int(num1) + num2)
print(num1 + str(num2))

Output result:

<class 'str'>
<class 'int'>
890
545345

About Boolean values

#False case
#Reshaped 0
#Floating point 0.0
#The blank string '' is converted to false '', which means space and blank
#Empty list []
#Empty tuple ()
#Empty dictionary {}
#Empty set()

The above is false and the rest is true

Example

print(bool(set()))
print(bool(0.0))

result

False
False

Other type conversion

Incomplete continuous update...

Topics: Python Programming