Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The Road to python-Basics 4

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

module

There are two modules

1. Standard module (library)

You can import directly and use it

2. Third-party modules (libraries)

Must download and install to use

Modules can also be called libraries

The initial two standard modules:

1. sys module

Examples:

import sys #Import sys module print (sys.path) Result: "'C:\\Users\\kk\\Documents\\python','C: \\python35.zip','C:\\DLLs','C:\\lib','C:\\Users\\kk\\Documents\\python']

Note: Self-defined module cannot be the same as imported library name

2. OS module

Examples:

import osos.system("dir")#Execute command without saving results cmd_r = os.popen("dir").read()print("-->",cmd_r) Create a directory os.mkdir("new_dir")

3. Self-defined modules

#Import your own Login module

The current path creates the Login module; Note: You can also put the Login.py in the site-packages directory under the python path, and you can import the module directly under other files.

#Determine Login.pyimport getpass #Reference getpass This module_username = "kk"_password = "123456"username = input("username:")password = getpass.getpass("password") #getpass function is to make the password not directly displayed as plain text if _username == username and _password == password:# print("Welcome user {name} login... ".format(name = username)) print("Welcome user %s login... " % username)else: print("error")print(username,password)

The current path creates learn01.py

#Import Login module created by yourself import Login

run

C:\Users\kk\Documents\python>python learn01.pyusername:kkpasswordWelcome user kk login... kk 123456

4 What is PyC?

Compiler language, typical example C language

Interpreter languages, current examples Ruby, Python

Java is a compiled language.

Python, like Java and C#, is also a virtual machine-based language.

When we type python hello.py on the command line, we activate Python's "interpreter,"

Tell the interpreter you need to get to work.

Python is a compiled language.

When python runs, the compiled result is stored in PyCodeObject in memory,

When the Python program finishes running, the Python compiler writes PyCodeObject back into the pyc file.

When the python program runs for the second time, first go to the hard disk to find the Pyc file, if you find it directly loaded, otherwise repeat the above process.

So we should locate PyCodeObject and pyc files like this, we say pyc files are actually a way to persist PyCodeObject.

Python data types

a, numbers

int (integer)

long (long integer)

float

complex (plural)

Note: type () View data type

b, Boolean value

true or false

1 or 0

c. String

"Hello,World"

6. Data operation

operator

comparison operation

assignment

logical operations

membership operation

identity operation

bit operation

The smallest unit that can be represented in a computer is a binary bit.

The smallest unit that can be stored in a computer is a binary bit.

8bit = byte

Learn Python from Alex Python Development Basics-1

7. bytes data type

ternary operation

8. Use of lists

a List, tuple operation

Examples:

names = ["zhangyang","guyun","xiangpeng","xuliangchen"]print(names[0])print (names[1:3])#slice python learn01.pyzhangyang['guyun', 'xiangpeng']names.pop("guyun")#If no parameter value is entered, the last value in the list is deleted by default names.append("kk")#Add a value names.insert at the end of the list (1,"kkk")#insert names in the middle of the list [2] = "kk"#modify names in the list.remove("kk")#Delete corresponding del names[1]#Another deletion method names.count(" kk")#Add duplicate values names2 = names.copy()#Copy

Example 2:

import copy#shallow copy a = [11,22,["kk","qq"],33]b = copy.copy(a)#copy a layer print(a)print(b)a[1] = 100a[2][0] = "kaixin"print(a)print(b)#deep copy-not easy to use a = [11,22,["kk","qq"], 33]b = copy.deepcopy(a)#Complete clone print(a)print(b)a[1] = 100a[2][0] = "kaixin"print(a)print(b) Result: [11, 22, ['kk', 'qq'], 33][11, 22, ['kk', 'qq'], 33][11, 100, ['kaixin', 'qq'], 33][11, 22, ['kaixin', 'qq'], 33][11, 22, ['kk', 'qq'], 33][11, 22, ['kk', 'qq'], 33][11, 100, ['kaixin', 'qq'], 33][11, 22, ['kk', 'qq'], 33]a = [11,22,["kk","qq"],33,44,55]print(a[0:-1:2])for i in a: print(i)#shallow copy second type #c = a[:]#shallow copy third type #d = list(a)#note: shallow copy is equivalent to the use of reference #shallow copy person = ["name",["saving",100]] #indicates name + deposit p1 = person[: p2 = list(person)p1[0] = "kk" #p1 represents a list of kk this person, they are husband and wife relationship p2[0] = "qq" #p2 represents a list of qq this person, They are husband and wife relationship #p1 and p2 husband and wife have common property 100 #If p1 costs 50 yuan, p2 will also be reduced by 50p1[1][1] = 50print(p1)print(p2)#tuple #names = ("a","b", 11)#print(names[0])

9. Shopping Cart Program

#program: shopping cart program project_list = [ #define product list ('Iphone',5800), ('Mac Pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), ('Alex Python', 120),]shopping_list = [] #Define shopping cart empty list of purchased items salary = input("Please enter your salary amount:") #Enter total amount if salary.isdigit():#Determine whether the entered amount is a number salary = int(salary)#Convert to integer if it is a number while True:# for item in project_list: Traverse the item list # print(project_list.index(item),item) for index,item in enumerate(project_list):#traverse list of items print(index,item)#Print number and corresponding item user_choice = input("Select item number to purchase>>>:")#Enter item number if user_choice.isdigit():#Determine whether the number entered is a digit user_choice = int(user_choice)#Convert to integer if numeric if user_choice

< len(project_list) and user_choice >

= 0:#Determine whether the number is less than the total number of commodity labels and greater than 0 p_item = project_list[user_choice]#Get item price if p_item[1] >>[(0, 'Spring'), (1, ' Summer'), (2, 'Fall'), (3, 'Winter')]#Note: Python isdigit() detects whether a string consists of numbers only.# Returns True if the string contains only numbers otherwise returns False.# #str = "123456"; # Only digit in this string#print str.isdigit();#>>True

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report