Notes - Day1 - Python Foundation 1

Keywords: Python Pycharm encoding Linux

I. directory

1. Introduction to Python

The founder of python, Guido van Rossum, is now a particularly frequent development language.

Main application areas:

  • Cloud Computing: The hottest language in cloud computing. Typical applications are OpenStack.
  • WEB Development: Many excellent WEB frameworks, many large websites are Python development, such as: Youtube, Dropbox, BT, Quora (China Knows), Douban, Knows, Google, Yahoo!, Facebook, NASA, Baidu, Tencent, Car House, Art Troupe, etc. Typical WEB frameworks are Django.
  • Scientific computing and artificial intelligence: typical libraries NumPy, SciPy, Matplotlib, Enthought libraries, pandas
  • System Operations and Maintenance: Operations and Maintenance Personnel Must Return to Language
  • Finance: Quantitative trading, financial analysis, in the field of financial engineering, Python is not only used, but also used the most, and its importance is increasing year by year. Reason: Python, as a dynamic language, has clear and simple language structure, rich library, mature and stable, scientific calculation and statistical analysis are very powerful, and its production efficiency is far higher than that of c,c++,java, especially good at strategy retrospective testing.
  • Graphic GUI: PyQT, WxPython,TkInter

What can we do after learning?

  • Python Development Engineer: Generally need to be proficient in Python programming language, have experience in using Django and other frameworks, no internship requirements.
  • Python Senior Engineer: If you want to go north, you need to be proficient in Linux/Unix platform and have English reading skills.
  • Web site development direction: familiar with the common Python framework of Web development, familiar with the operation of Mysql class database.
  • SEO Engineer: Develop and improve SEO related software for oneself or company to achieve automated search engine optimization and daily repetitive work.
  • Python Automated Testing: Familiar with automated processes, methods and the use of commonly used modules, have the ability to read and write in English.
  • Linux Operations and Maintenance Engineer: Linux server management, data analysis, automated processing tasks, analysis of website logs, timing plan management, free hands.
  • Python Game Development Engineer: Logic development and processing of the back-end server of online games, experience in using large databases, like to work in game-related work.
  • Python self-study enthusiasts: you can develop some small software and applications, with graphical interface software, to facilitate daily work.

2.Python History

slightly

3.Python 2 or 3?

Although 2 and 3 are somewhat different, they must be used more and more with the passage of time, so we should learn 3 well.

4. Installation of Python

windows downloads programs directly for installation. Linux and Apple default to python. If you need a new version, you need to download and install it.

All my exercise notes were completed in Pycharm 5.0.3, and the python version was 3.6.

5. Write the first Hello World program

6. Variable/character encoding

a. Variables are variable. The definition rules of variables in python are as follows:

  • Variable names can only be any combination of letters, numbers, or underscores
  • The first character of a variable name cannot be a number
  • The following keywords cannot be declared as variable names ['and','as','assert','break','class','continue','def','del','elif','else','except','exec','last','for','from','global','if','import','in','is','lambda','not','or','pass','print','return','try','while']

b. A brief history of character encoding:

USA: 1963 ASCII (127 characters, 1 byte)

China: 1980 GB2312 (7445 Chinese characters, including 6763 Chinese characters and 682 other symbols)

1993 GB13000 (containing 20902 Chinese characters)

1995 GBK 1.0 (21003 Chinese characters)

2000 GB18030 (containing 70244 Chinese characters)

World: 1991 unicode ('Universal Code'is also a unified code, usually 2 bytes, 4 bytes for complex Chinese characters)

UTF-8 (Variable Length Character Coding)

The character encoding conversion process of the string in Python 2 is as follows:

Byte string - > decode ('original character encoding') - - > Unicode string - > encode ('new character encoding') - - > byte string

The string defined in Python 3 is unicode by default, so it does not need to be decoded first and can be directly encoded into a new character encoding:

String - > encode ('new character encoding') - - > byte string

7. User input

That is, interactive reminders require users to enter some information, and then print the corresponding results.

8. module

The power of Python is that it has a very rich and powerful standard library and third-party library, almost any function you want to achieve has the corresponding Python library support.

9. What is PyC

We say that pyc files are actually a way of persistent preservation of PyCodeObject

10. Initial Knowledge of Data Types

  • Numbers: Shaping, Floating Point, Complex
  • Boolean: 1 (True) is true, 0 (False) is false
  • Character string:
  • List: Index, Slice, Added, Deleted, Length, Circulation, Containment
  • Tuples: variable lists
  • Dictionary: Disorder
  • Bytes: bytes

11. Data Operation

  • Arithmetic operations (+ (addition), -(subtraction), * (multiplication), /(division), (modulus), ** (power), //(division))
  • Comparisons (== (equal),! = (not equal), <> (not equal), > (greater), < (less than), >=(), <=())
  • Assignment operations (=, += (self-increasing), -= (self-decreasing), *= (self-multiplying),%= (self-modulus), **=(), //=())
  • Logical operations (and, or, not)
  • Membership operations (in, not in)
  • Identity operations (is, is not (is the same object)?
  • Ternary operation (result = value 1 if condition else value 2 (if condition is true result = value 1, if condition is false result = value 2))

Learn more about: Slamming here

12. The expression if... Else statement

13. Expressions for and wile loops

14.break and cotinue

2. Exercise

1. slightly 2. slightly 3. slightly 4. slightly

5. Write our first hello world program

In linux: a. Create a new hello_world.py file

b. Edit the file # vim hello_world.py (add #!/ usr/bin/env python to the header of the file, specify the interpreter, and add a line'pring'Hello world')

b. Add executable permissions to the file # chmod a+x hello_world.py to execute through # python hello_world.py

Note, all my exercises were done in Pycharm.

#pycharm 5.0.3(python3.6)
print('hello world!')
Hello_world code
hello world!
Process finished with exit code 0
Operation result

6. variable

#pycharm 5.0.3(python3.6)
names ='Felix'#Declarative Definition Assignment of Variables
print('Print the result name of the variable:',names)
Variable code
Print variable results Felix

Process finished with exit code 0
Operation result

Character encoding

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#pycharm 5.0.3(python2.6)
#Byte string-->decode('The original character encoding')-->Unicode Character string-->encode('New character encoding')-->Byte string
utf_8_a = 'test'
print(utf_8_a,type(utf_8_a),len(utf_8_a))  #Printing utf_8_a Content, Type and Length
unicode_a = utf_8_a.decode('utf-8') #Yes utf_8_a Decoding unicode
print(unicode_a,type(unicode_a),len(unicode_a))
gbk_a = utf_8_a.decode('utf-8').encode('gbk') #Decoding before encoding gbk
print(gbk_a,type(gbk_a),len(gbk_a)) #Printing gbk_a Memory, Type and Length
print(gbk_a.decode('gbk'))
Python 2.6 run code
('\xe6\xb5\x8b\xe8\xaf\x95', <type 'str'>, 6)
(u'\u6d4b\u8bd5', <type 'unicode'>, 2)
('\xb2\xe2\xca\xd4', <type 'str'>, 4)
//test

Process finished with exit code 0
Operation result
#pycharm 5.0.3(python3.6)
#Python3 The string defined in the unicode,Therefore, it is not necessary to decode first, but can be directly encoded into a new character encoding:
#Character string-->encode('New character encoding')-->Byte string
utf_8_a = 'test'
print(utf_8_a,type(utf_8_a),len(utf_8_a))  #Printing utf_8_a Content, Type and Length
gbk_a = utf_8_a.encode('gbk')
print(gbk_a.decode('gbk'),type(gbk_a),len(gbk_a))
Python 3.6 run code
test <class 'str'> 2
//test <class 'bytes'> 4

Process finished with exit code 0
Operation result

7. User input

#pycharm 5.0.3(python3.6)
name = input('Please input your name:')
#If you want the password you entered to be invisible, you can use it import getpass Modular
import getpass #Usually the calling module is written at the top
pwd = input('Your passwd:')
print('Hello,'+name) #String connections can be used“+"(Plus sign
print('Your password is:',pwd)
Input code
Please input your name:Felix
Your passwd:123
Hello,Felix
 Your password is 123.

Process finished with exit code 0
Operation result

8. module

import sys
print(sys.path) #Print environment variables
print(sys.argv) #Actually, what you print is the relative path, because pycharm There is a name, so the absolute path is printed directly.


import os
#print(os.path)
#cmd_res = os.system("dir")   #Execute commands without saving results
cmd_res = os.popen("dir").read() #No addition read,It's just stored in memory and needs to be used read read
print("-->",cmd_res)


#os.mkdir("new_dir") #Create directory
#import login #Find the module first to find in the current directory, if not error, solve, solve with the directory, or modify the environment variables.
Module code

9. slightly

10. Data type

Slightly

11. Data Operation

Slightly

12.if ...else

#pycharm 5.0.3(python3.6)
#Requirement: prompt user to enter username password
#Realization: User enters username password and verifies it(name = 'Felix',pwd = '123')
#      If the error occurs, the user name and password are output
#      If successful, the output is welcome. XXX!
import getpass #If you want the password you entered to be invisible, you can use it import getpass Modular
name = input('Please input your name:')
pwd =  int(input('Please input your pwd: '))
# pwd = int(getpass.getpass('Please input your pwd: ')) #You can hide password hints. Here the test is problematic. The password is not finished after entering.

if name == 'Felix' and pwd ==123:
    print('Welcome ',name)
else:
    print('Your name or pwd error')
User login validation game scenarios
Please input your name:Felix
Please input your pwd: 123
Welcome  Felix

Process finished with exit code 0
Operation result
#pycharm 5.0.3(python3.6)
#Requirements: Set your age in the program, and then start the program to let users guess.
#Realization: After user input, according to his input prompt user input is correct, if the error, the prompt is guessed big or small
age_of_oldboy = 56
guess_age = input("guess age:")
if guess_age == age_of_oldboy:
    print("Yes,you got it.")
elif int(guess_age) > age_of_oldboy:
    print("think smaller")
else:
    print("think bigger")
#The above version can only enter one age at a time and need to run the program here.
Guess Age Game Scene
guess age:28
think bigger

Process finished with exit code 0
Operation result

Combining with the conditions, the user can constantly guess the age by using the cycle, but only give up to three chances, and then quit the program if the guess is wrong:

#pycharm 5.0.3(python3.6)
#while Circulation mode
age_of_oldboy = 56
count = 0
while count <3:
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy:
        print("Yes,you got it.")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller")
    else:
        print("think bigger")
    count +=1
else:
    print("You have tried too many times..fuck off")
Guess age (while cycle)
guess age:20
think bigger
guess age:12
think bigger
guess age:30
think bigger
You have tried too many times..fuck off

Process finished with exit code 0
Operation result
#pycharm 5.0.3(python3.6)
#for Circulation mode
age_of_oldboy = 56
count = 0
for i in range(3) :
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy:
        print("Yes,you got it.")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller")
    else:
        print("think bigger")
else:
    print("You have tried too many times..fuck off")
Guess age (for cycle)
guess age:30
think bigger
guess age:56
Yes,you got it.

Process finished with exit code 0
Operation result

 

13. cycle

   for loop

#pycharm 5.0.3(python3.6)
#Cyclic Printing 1 to 9
for i in range(10):
    print(i)
for loop
0
1
2
3
4
5
6
7
8
9

Process finished with exit code 0
Operation result
#pycharm 5.0.3(python3.6)
#Requirement 1, circular printing 1 to 9, but encounter less than 5 cycles do not go, directly jump into the next cycle
#Here's where you can use it. continue
for i in range(10):
    if i<5:
        continue #Don't go down, go straight to the next cycle
    print(i)
continue
5
6
7
8
9

Process finished with exit code 0
Operation result
#pycharm 5.0.3(python3.6)
#Needs 2: It's still the above program, but when it encounters more than 5 cycles, it doesn't go away and exits directly.
for i in range(10):
    if i >5:
        break #Don't go down, just jump out of the whole cycle.
    print(i)
break
0
1
2
3
4
5

Process finished with exit code 0
Operation result

  while loop

#pycharm 5.0.3(python3.6)
#Dead cycle
count = 0
while True:
    print('Wait...')
    count +=1
Dead cycle
#pycharm 5.0.3(python3.6)
#Dead cycle
count = 0
while True:
    print('Wait...')
    count +=1

    if count == 5:
        print('Interrupt cycle')
        break
Interrupt Dead Cycle

 

14. Interruption. Practice in 13 cycles

 

3. Reference

http://www.cnblogs.com/alex3714/articles/5717620.html <Blogger Blog Records>

http://www.runoob.com/

Note: This note is based on "Python 14 Baidu Video for Senior Boys", address: https://chuanke.baidu.com/v3628575-182242-1134376.html

Posted by Hitman2oo2 on Sun, 26 May 2019 17:29:25 -0700