Getting Started with Python Learning Python

Getting Started with Python Chapter II

2.1 Installation Environment

2.1.1 download interpreter:

  • py2.7.16 (2020 Official no longer maintained)

  • py3.6.8 (recommended installation)

    1, download the interpreter must go to the official website to download, https://www.python.org

2. Select python3 version Download

3, the 64-bit download installation package python3

4. Select the download version of python2

5, the 64-bit download installation package python2

2.1.2 mounting interpreter Python36

1. Download the installation package good, playing the first page, check on the box "added to the environment variable"

2. Click Custom Installation

3, keep the default of all the checkboxes, and click Next

4, check the installation for all users, click on the install path address change the installation path

5, change the installation path, as far as possible installed in the root directory (D: \ Python36)

6. Click on the Install button

7, installation

8, the installation is complete

9, view the environment variables, Computer - Properties - Advanced System Settings - Environment Variables - System Variables - Path

Because the first step is checked before added to the environment variables, so you can view the display system variable Path in respect of the installation path python36.

2.1.3 python 3.6.8 to detect whether the installation is successful

1, the system environment variables after the completion of the addition can detect python 3.6.8 is installed successfully, click Start - Run, search box, type: cmd

2, the input terminal Enter to open cmd

3, the opening of the transport terminal displays the input information is represented by the following python installation was successful.

2.1.4 adding environment variables, in order to quickly find the python interpreter in the future

If the transport in the terminal type python Tip: python command is not an internal or external command, the program can not run

On behalf of the environment variable is not added to the python installation, you need to do the following:

Computer - Properties - Advanced System Settings - Environment Variables - System variables, double-click Path, will python36 installation path D: \ Python36 and D: \ Python36 \ Scripts added, all in English of ";" separated after clicking OK.

Finally, re-open the terminal and type python python interactive displays a prompt.

2.1.5 python2 installation

1. Download good python2 installation package, the default choice is to install for all users, just click Next

2. Click Change the installation path, the proposed installation under the root directory in D (D: \ Python27)

3. After changing the installation path

4. Installation Options page there is added to the environment variable options suggested here do not choose, keep the default, click Next

Installation python27 and python36 as there is added to the environment variable option installed, check if the installation will be automatically added to the user environment variable, so if you replace the current computer after the user can not use python, so as not to allow other users to use, so the installation process is recommended not to opt in to the environmental variables, such as the installation is complete add the environment variable to the system environment variables inside the manual, must not be added to the user environment variables, so replacement is also easy to use after a user logs the same python interpreter.

5, the installation

6, the installation is complete

7, open a terminal, an input python2

8, add the environment variable

Computer - Properties - Advanced System Settings - Environment Variables - System variables, double-click Path, the installation path D python27 of: \ Python27 added to the English ";" separated after clicking OK.

2.1.6 python2 issues of coexistence and python3

1 Note: Whether or python3 python2, after installation in the path of the installation files folder will appear in a python.exe!

2, the path of the file are added to the system environment variables, type python after the transport system because the use python2 or to python3 tell, therefore the error in the terminal

3, then should before adding environment variables, python.exe python2 installation path folder or rename python2.exe python27.exe, python.exe will python3 installation path folder rename python3. exe or python36.exe, and the file path are added to the renamed system environment variables

4. After adding an environment variable or continue entering python2 python3 in the original terminal or will complain

5, this time to re-open the terminal and type python2 or python27 will call python2 environment, input or python36 python3 will call python3 environment, just type python on the error.

2.1.7 IDLE integrated development environment

1, IDLE Profile

IDLE is a python package comes with an integrated development environment, you can easily create, run, and debug python programs. After installation is complete python3 python2 or automatically installed.

2, how to open IDLE: Start - Run - the search box enter: idle

3. Click IDLE (Python GUI) to open the shell python2

4. Click IDLE (Python 3.6 64-bit) to open the shell python3

5, after starting IDLE to see the python shell, it can execute commands within the python IDLE through. IDLE also comes with an editor for editing python program (or script); there is an interactive Python interpreter to explain the implementation of the statement; there is a Python script debugger to debug.

The figure shows the statement to execute Python

2.1.8 The first script (a file)

  • Create a root directory in the D test.txt file, open and enter the following code and then close

    print('你好')
  • Open computer terminal: Function key + R then type: cmd

  • Enter the command: path to the script interpreter path

    C:\Users\amssy>python36 D:\test.txt
    你好
    #更改文件的后缀名为.txxxxx 或者 .php 都行
    C:\Users\amssy>python36 D:\test.txxxxx
    你好
    #潜规则,python代码文件以 .py 后缀
    C:\Users\amssy>python36 D:\test.py
    你好

2.2 encoding

2.2.1 code base

  • ascii

    A character occupies 1 byte (8 bits)

  • unicode

    A character with 4 bytes (32-bit), the world's universal language, now only uses 21,

    When storage space is too large

  • utf-8 (recommended)

    For unicode compression, accounting for a few on the show a few, the number of bits is a multiple of 8 8

    That solves the problem of global languages, but also solve the storage space when the problem of excessive

    Least one byte = 8 bits, with up to 4 bytes = 32 bits.

    Chinese: 3 bytes = 24 represents

  • gbk

  • gb2312

2.2.2 python coding-related

For the default Python interpreter code:

  • py2: ascii
  • py3: utf-8

If you want to modify the default python2 coding, may be added in the first line of the file header, the following is the code input, that is no longer use the default ASCII code, designated as the python interpreter code utf-8 encoded.

# -*- coding:utf-8 -*-

note:

For operating files, according to what is written in code, you must use what encoding to open.

+ Coding decoding to be consistent.

2.3 Interpreter

File: a.py

#!/usr/bin/env python    # 在linux中指定解释器的路径
# -*- coding:utf-8 -*-

print("你好")

Run: interpreter file path

There is a special method of execution on linux:

  • Give permission to an executable file
  • The first line will automatically go ./a.py file = / usr / bin / env python a.py

2.4 Output

print(你想要输出的东西)

special:

  • py2: print "hello"
  • py3: print ( "Hello")
  • python2.7 which support the above two

2.5 Simple data types

'alex' / "李杰" / '''asdg''' / """dfsf""" ,一般称为 字符串
666 ,一般称为 数字/整型
True / False ,一般称为 布尔型
# 如:
print('你好') # 输出字符串:你好
print(你好) # 此行会报错:NameError:name '你好' is not defined
print('你"好') # 输出:你"好
print("窗前明月光,地上鞋两双") # 输出显示一行
print("""窗前明月光,
地上鞋两双""")  # 输出会换行,一般用作多行输出

print("窗前明月光,")
print("地上鞋两双") # 这两行输出也会换行,但如何是一篇文章就不可能用此方法,要用三引号

print(666) # 输出数字:666
print('666') # 输出字符串(长得像数字的字符串):666

print(True) # 输出:True
print(False) # 输出:False
  • 1, the character string (it must be in quotation marks English state)
    • apostrophe
    • Double quotes
    • Triple quotes
  • 2, integer
  • 3, boolean

2.6 Variable

# 需求:将 '钓鱼要钓刀鱼,刀鱼要到岛上钓。'打印3遍
# 普通方法:费时费力
print('钓鱼要钓刀鱼,刀鱼要到岛上钓。')
print('钓鱼要钓刀鱼,刀鱼要到岛上钓。')
print('钓鱼要钓刀鱼,刀鱼要到岛上钓。')

# 高级方法:
#     创建一个变量content,并且给变量赋值,值等于'钓鱼要钓刀鱼,刀鱼要到岛上钓。'字符串。
content = '钓鱼要钓刀鱼,刀鱼要到岛上钓。'
print(content)

content = '钓鱼要钓刀鱼,刀鱼要到岛上钓。'
content = 666
print(content) # 这里输出结果是666,因为content重新被赋值了

999 = '钓鱼要钓刀鱼,刀鱼要到岛上钓。'
999 = 666
print(content) # 执行会报错:SyntaxError:can't assign to literal
  • Variable requirements:

    • 1, the variable name can only contain: letter / number / underscore

    • 2, the numbers do not begin with

    • 3, can not be a python keyword

      ​ ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

      A total of 33 keywords, in addition to 'True', 'False' and 'None', the other keywords are lowercase.

      Note: Python is a dynamic language, constantly changing according to the time, the keyword list is likely to change in the future.

    • 4, recommended that:

      • See EENOW name: name = 'alex' age = 18
        with an underscore (recommended): alex_dad = 'Jie'
      • Camel named (Java in common): AlexDad = 'Jie'

  • Q: Why have variable?

    Create a "nickname" for a value addition after the adoption of resolution can be called directly when in use.

The comprehensive exercises 2.7

# 第一题
age = 18
new_age = age + 1
print(new_age)

# 第二题
name = "alex"
new_name = name + 'sb'
print(new_name)

# 第三题
age = "666"
new_age = age + "666"
print(new_age)

# 第四题
age = "666"
new_age = age + 666
print(new_age) # 运行报错:TypeError:must be str,not int

# 第五题
age = 6
new_age = age * 2
print(new_age)

# 第六题(特殊)
name = "alex"
new_name = name * 2
print(new_name)

# 第七题
age = 18
value = age >= 19  # >=大于等于
print(value)

# 第八题
_ = 9
_9 = 9
9name = 'alex'  # 报错:SyntaxError: invalid syntax
True = 9   # 报错:SyntaxError: can't assign to keyword
print = 666  # print虽然不是关键字,但是也不能作为变量名,运行会报错

2.8 input

input('请输入你的名字:') 
# 运行时要求用户输入,用户输入后程序就结束了

user_name = input('请输入你的名字:') 
# 括号内的内容为提示语,也可以不加提示语 user_name = input()
message = user_name + "烧饼"
print(message)

note:

  • Enter input string will always get content
  • py version differences:
    • py2: name = raw_input ( 'Enter your name:')
    • py3: name = input ( 'Enter name')

Example:

user_name = input('请输入你的姓名:') 
password = input('请输入你的密码:') 
content = "你的用户名是:" + user_name + ";你的密码是:" + password
# content = "你的用户名是:" + "alex" + ";你的密码是:" + "123"   
print(content)

2.9 Notes

The process of writing code, "#" and call back the contents of the comment, the representative of this line of code does not work, it is not the same, skip this line of code when the code is executed.

  • Single-line comments: starting with # content
  • Multi-line comments: written in the [ "" "" "" or '' '' '' (three marks)] Content
user_name = input('请输入你的姓名:') 
password = input('请输入你的密码:') 

# 用户名和密码拼接  ——这是单行注释
content = "你的用户名是:" + user_name + ";你的密码是:" + password

#输出用户名和密码拼接的结果
print(content)


"""
写在这里的就
是多行注释
"""

After the code is written, then more strongly recommends to write a commentary not only to others, but also to look at yourself!

Conditional 2.10

1, a primary condition if ... else statement

"If: ... else: ..." statement carriage return will automatically indent Editor tool pycharm after entering a colon, usually four spaces, there is no automatic indentation by in Notepad or other editor 4 spaces, or press Tab, space, and Tab key to try not to mix, so unknown error occurred.

# 实现一个功能:让用户输入性别,如果是 男,则输出:再见;如果是 女,则输出:来啊来啊
gender = input("请输入性别:")

"""
如果是男生:打印再见
否则:打印来啊来啊
"""

if gender == "男":
    print('再见')
else:
    print('来啊来啊')

​ 2、if...elif...else

# 实现一个功能:让用户输入性别,如果是 男,则输出:再见;如果是 女,则输出:聊聊啊;否则:滚
gender = input("请输入性别:")

"""
如果是男生:打印再见
如果是女生:打印聊聊啊
否则:打印滚
"""

if gender == "男":
    print('再见')
elif gender == "女":
    print('聊聊啊')
else:
    print('滚')
# 实现一个功能:让用户输入性别,如果是 男,则输出:再见;如果是 女,则输出:聊聊啊;如果是 人妖,则输出:找alex去,他也是;否则:滚
gender = input("请输入性别:")

"""
如果是男生:打印再见
如果是女生:打印聊聊啊
如果是人妖:打印找alex去,他也是
否则:打印滚
"""

if gender == "男":
    print('再见')
elif gender == "女":
    print('聊聊啊')
elif gender == "人妖":
    print('找alex去,他也是')
else:
    print('滚')
print('end') # 
gender = input("请输入性别:")
if gender == "男":
    print('再见')
elif gender == "女":
    print('聊聊啊')
elif gender == "人妖":
    print('找alex去,他也是')
else:
    print('滚')
print('end')
# 无论输入什么,最后都会打印 end

3, the simplest

gender = input("请输入性别:") # 女
if gender == "男":
    print('再见')
# 结果不会打印

4, exercises

# 第一题:让用户输入一个数字,猜:如果数字 > 50,则输出:大了; 如果数字 < 50,则输出:小了。
num = input('请输入一个数字:')
number = int(num) # 因为用户输入的都是字符串,所以这里需要使用int()转换成整型才可做比较。
if number > 50:
    print('大了')
else:
    print('小了')


# 第二题:用户名密码登录
username = input('请输入用户名:')
password = input('请输入密码:')

if username == 'alex' and password == 'oldboy':
    print('欢迎登录')
else:
    print('用户名或密码错误')

2.11 Content summary

  • Computer base (FIG)
  • Interpreter installation
    • py2 & py3 coexist: you can not find what you want, such as the environment, you can change the name of the executable .exe file, and then add environment variables
  • coding
    • Three coding difference
    • What it open with what saved, always saved on the hard disk are 01010101
    • py2 & py3
  • Export
  • type of data
    • String
    • Integer
    • Boolean value
  • variable
  • Entry
  • Note
  • Conditional statements
  • Gift:
    • number = int ( '666') # string to an integer
    • name == 'alex' and password == '123' # a = is an assignment, two relatively =
    • result = "xxx"=='alex' and 213=='123' # False
  • remind:
    • Typing slow - Jinshan typing pass
    • English does not - translation software and look at the code notes
    • Notes error - error and finishing solutions

2.12 pycharm use

  • pycharm download and install your own Baidu

  • use:

    1, open pycharm, for the first time click Create New Project (create a new project)

2. Create a new project!

3, a new project after setting the prompt click Create

4, create a file on the project

5, enter the file name, you can find the beginning in order to facilitate the use of Chinese, later suggested that development must be in English

6, the input code file, a blank area right click Run 'xxx' run the file

7, run the file results

8. Adjust font size: Click the file - settings

9. Other features: Click file

10, quickly open the folder where the file

11, pycharm automatically generates header code

File - Settings - Editor - File and Code Templates - Python Script - Enter code - Click OK

12, commonly used shortcuts

Ctrl + D to copy the selected area or row

Ctrl + Y Delete selected rows

Ctrl + / line comment / uncomment

Ctrl + Shift + / block comment

Guess you like

Origin www.cnblogs.com/duncan1863/p/11410407.html
Recommended