Python learning - Beginners Introduction 2

Module 1 Introduction

Standard libraries and third party modules

(1) standard library

#!/usr/bin/env python

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

import sys

print(sys.path)

"""

'/Users/develop/Documents/Python_study/s14/day2', 

'/Users/develop/Documents/Python_study/s14', 

'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', 

'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', 

'/Users/develop/Documents/Python_study/s14/venv/lib/python3.7/site-packages', 

'/Users/develop/Documents/Python_study/s14/venv/lib/python3.7/site-packages/setuptools-40.8.0-py3.7.egg',

'/Users/develop/Documents/Python_study/s14/venv/lib/python3.7/site-packages/pip-19.0.3-py3.7.egg']

"""

python standard library typically exists socket.py network of the /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7 e.g., re module is a regular expression

print (sys.argv [2]) Enter file name .py develop $ python3 the terminal 123 may output 2

'''

Input module .py 1 2 3

Will print 2

zzhdeMacBook-Pro: day2 develop $ python3 module .py 1 2 3

['/Users/develop/Documents/Python_study/s14/day2', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',

'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',

'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']

2

'''

os .mkdir ( "new") will create a new folder in the current directory

 

(2) third-party modules

'''

# References Third-party choose to start in the current directory

# 1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7 goes on to write their own modules

# 2. Add Path

'''

For example: In the current directory, create a file guess_age.py copy put in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7

import guess_age.py # directly reads guess_age.py inside the program

'''

guess_age:50

think bigger!

guess_age:80

think smaller...

guess_age:60

think smaller...

you have tried too many times...fuck off

'''

Two pyc

python is an interpreted language, but the resulting pyc is doing it, c should be complied abbreviation fishes ah

The computer can not recognize the high-level language, it is time to run through a high-level language to be translated into computer language recognition

Like Jave explained after the first compiled during the operation of the python. python with Java virtual machine is based on language

pyc file is a real compiler to compile results

Process is as follows:

python run time, compile the results will be stored in memory PyCodeObject, when the end of the python run, pyhton

The compiler will PyCodeObject pyc written back in.

When the second run python program, the program to your hard disk looking for pyc file, if found directly loaded, otherwise repeat the above process (the first time did not find repeats)

If pyc already exists, modify the source code, how to do. Updated from time to determine whether re-compiled pyc

For example, after import guess_age will generate /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7'/__pycache__ guess_age.cpython-37.pyc

 

Three strings

= name "My name IS Jerry"
Print ( "----- grammar ----")
Print ( "capitalize ()")
Print (name.capitalize ()) # capitalize the first letter lowercase other change will result: My Jerry iS name
Print ( "COUNT")
Print (name.count ( "E")) appear #E number
Print ( "Center")
Print (name.center (50, "*")) # print a total of 50 characters the name is not enough to put the middle, both sides of the filling *: ***************** ***************** My name iS Jerry

Print ( "endsWith")
Print (name.endswith ( "rry")) # determine whether to end rry return True

Print ( "expandtabs ()")
NAME1 = "My \ tname IS Jerry"
Print (name1.expandtabs (TabSize = 10 )) # string has time tab, the number of spaces converted to tab. The results: My name IS Jerry

Print ( "the Find")
Print (name.find ( "IS")) # returns the subscript 8
Print (name [name. find ( "is"): 10]) # with a similar list can be sliced.

The results: IS Print ( "fomat")
= NAME2 "My name IS {name} and {Age} AM I"
Print (name2.format (name = "Jerry", Age = 25))
Print (name2.format_map ({ "name": "Jerry", "Age ": 25})) # dictionary form of

print (" index ")
print (" isalnum ") # is not Arabic numerals (English characters plus 0-9)
print (" isalpha ") # whether plain English characters
print (" whether isdigit integer ")
print (" isIdentifier is not a legal variable name ")
print (" islower is not a lowercase ")
print (" whether isnumeric digital ")
print (" 33.3 ".isnumeric ()) # only digital
print ( "33.3" .isdigit ()) # common
print ( "isspaces is a space")
Print ( "istitle whether to start with a capital")
Print ( "isupper whether all uppercase")
Print ( "the Join")
NAME4 = "My name is Jerry "
print(name4.join(["1","2","3","4"]))

print("+".join(["1","2","3"]))

print("ljust")
print (name4.ljust (50, "* ")) # total output is less than 50 characters, then back to the rjust not * reverse

print ( "lower") # to uppercase lowercase
print ( "upper") # becomes the lower case uppercase
print ( "lstrip") # strip on both sides of the spaces will be removed / is to remove the wrap lstrip rstrip is removed to the right of the left

print ( "maketrans the corresponding character exchange, the same corresponds to the last one")
P = str.maketrans ( "jergg" , "12345")
Print ( "jergy" .translate (P))

Print ( "replace all may alternatively replace the default number of alternative transmission")
Print ( "Jerrry" .replace ( "R & lt", "R & lt", 2))

print ( "rfind left to find the last face value of the index")
Print ( "Jerry" .rfind ( "r"))

Print ( "Split according to what the interception, the default is the space")
Print ( "Jerry iS" .split ())
Print ( "+. 1. 4. 3 + 2 +" .split ( "+"))

Print ( "The splitlines taken by newline")
Print ( "2 +. 1 \ n-+ +. 4. 3 ".splitlines ())

Print (" What startsWith Press Start ")

Print (" swapcase invert case ")
Print (" Jerry ".swapcase ())

Print (" Each title the first letter capitalized character change ")
print("my name is".title())

print("zfill")
print("Jerry".zfill(50))

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhenhua37/p/11434343.html