Python data types and what the module means

Source: Internet
Author: User

Print (Sys.path) # printing environment Variables

Print (SYS.ARGV) # Printing relative path

Print (sys.argv[1]) # prints the corresponding parameters

1. in python , sometimes the OS module is imported , which means that when interacting with the system, the os module is used

For example:

Cmd_res=os.system ("dir") # displays the directory under the current path

Print (cmd_res) # result is 0, indicating that the command performs the correct

Os.mkdir ("New_dir") # indicates that a new_dir directory is created under the current path

2. Data type:

int: integer type, no long integer in Python , type (2**32)

float: Floating-point,3.24,5.31e4,E = 10**4

Boolean type: d=a if a>b else C with 1 and 0

3. Conversion between the binaries

Conversion of binary and hexadecimal

the text is all Unicode, binary is bytes ( audio, video are binary )

in python3 binary (bytes) and strings are not spliced

binary --------> string requires decode ( decoding )

string ---------> binary requires encode ( encoding )

For example:

Enter the following code in the Python3

msg="I love Beijing"
Print (Msg.encode (encoding="Utf-8")) #下面为编译结果

E:\python3.5.2\python3.exe e:/workspace/s14/day1/codeing.py

B ' \xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac '

The above is expressed as changing the string into binary code

msg="I love Beijing"
Print (Msg.encode (encoding="Utf-8"). Decode (encoding="Utf-8") #再将二进制转换为字符串

4. About the module meaning of the slice

names=["[email protected] Beijing","x Shanghai", ["Wuhan","Henan"],"x guangzhou","6 Tianjin" ]
Names.append ("Shaanxi") #在列表最后增加陕西
Names.insert (4,"Xian") #在列表对应位置4插入西安

Print (names[0],names[2]) #取0和2位置的对应值

Print (Name[1:3]) #取1位置到3位置之前的所有值, excluding 3

Print (name[-1]) #取最后一个值

Print (name[-2:]) #取导数第二个值到最后的值

names[2]= " Henan " #修改位置2的值为河南

Names.remove (" Xian ") #去除西安

Del Names[1] #删除位置1的值

Print (Names.index (" Wuhan ")) #打印武汉对应的位置

Print (Names (Names.index (" Wuhan "))) #取出对应位置的城市

Print (Names.count (" Xian ")) #统计有几个西安

Names.clear () #清空列表

Names.reverse () #反转列表顺序

Names.sort () #排序, special characters > Numbers > Uppercase letters > lowercase letters

names2=[1,2,3,4] #定义一个新列表

Names.extend (Names2) #扩展, merger

Names2=names.copy () #浅copy

Names2=copy.deepcopy (names) #深copy

Skip cut print (Names[:-1:2]) #表示不取最后一个值, take a value every other

tuple read-only list names= ("", "", "", "") only count and index

Shopping Cart Assignments

Requirement:1 After starting the program, enter the payroll and then print the list of items

2 Allow users to print a list of items based on their product number

3. The user selects the goods, checks whether the balance is sufficient, if enough to buy the goods directly, if not enough, exit the program

4. You can exit at any time and print out the balance of the goods you have purchased.

#!/user/bin/python3
#-*-Coding:utf-8-*-
product_list=[("red bine"), ("apples", "Ten"), ("table", "+"), ("Bike" , 325), ("Computer", 4555)]
shopping_list=[""]
salary=input ("Please enter your salary:")
if salary.isdigit ():
Salary=int (Salary)
While True:
For index,item in Enumerate (product_list):
print (Index,item)
user_choice=input ("Please enter what do you want to buy?") ")
if user_choice.isdigit ():
user_choice=int (User_choice)
if user_choice>=0 and User_choice<len (product_list):
P_item=product_list[user_choice]
if salary>=p_item[1]:
shopping_list.append (P_item)
Salary=salary-p_item[1]
Print ("added%s into your shopping cart and your balance is \033[40;1m%s\033[0m]% (p_item,salary))
Else:
For p in shopping_list:
Print (p)
print ("\033[21;1myour balance is%s\033[0m"%salary)
exit ()
Else:
Print ("These has not product")

elif user_choice=="q":
Print ("Exit ....")

Else:
print ("invailed option")

Python data types and what the module means

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.