Python Learning note day3 (data type)

Source: Internet
Author: User
Tags string methods

Python data type:
int type
Float decimal Type
String strings
Boolean type a = True B = False

1. A list, also known as an array or a list or array. Its expression is rendered by subscript or index or number.
students=[' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ']
Print (Students[0])
Note: The subscript for the first element is 0, and the last element is subscript 1
Cities = []
#增加: (Two ways)
Cities.append (' Beijing ') adds an element at the end of the list
Cities.insert (0, ' Shanghai ') add an element at the specified position in the list

#删除:
Cities.pop (1) Delete the element at the specified position
Cities.remove (' Beijing ') delete the specified element
Cities.clear () Clear List
Del cities (0) Delete the specified position element

#修改:
cities[-1]= ' Nanjing ' modifies the element at the specified position, if the specified position does not exist will be an error

#查询:
Print (cities[1]) Query the specified position element
Print (Cities.index (' Guangzhou ')) gets the subscript of the element, and if no specified element is found, an error is given.
Print (Cities.count (' Guangzhou ')) gets the number of occurrences of the specified element in the list

#.inverse () reverse, reverse the list
new_lis=[' Hello ', ' world '
Print (New_lis.verse ())

#.sort
nums=[3,2,5,7,1,0]
Nums.sort () #排序, ascending
Nums.sort (reverse=true) descending cities2=[' Wuhan '] #print (cities + cities2) Merge list
#print (cities)  copy 3 times

#多维数组
words=[' view ', ' Code ', ' Tools ',
[' Price ', ' num ', 198,
[' Hello ', ' USA ', ' Eat chicken ']
] ]
Print (words[-1][-1][1])

#切片, a slice is a way for a list to take a value (the slice operation also applies to a string)
nums=[' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ']
Print (Nums[0])
Print (Nums[1:3]) Gu Tou Regardless of the tail, starting from the 1th to take, take to 第3-1个 so far
Print (nums[1:]) If you start from a subscript, take the end of the end, then the end of the subscript can be omitted to write
Print (Nums[:2]) If you take it from scratch and take it to the end of one of the following subscripts, the subscript at the beginning may not be written
Print (nums[:]) takes all

Lis = List (range (1,21))
Print (LIS)
Print (Lis[::2]) step, one at a time
Print (Lis[::-2]) step, one at a time
If the step is positive, start with a value from left to right
If the step size is negative, start from the right to the left to take a value
Print (lis[::-1]) #反转list

Practice:

#校验用户名是否合法:
 # Enter account #input 
# If the account exists, the hint has been registered, if it does not exist, let him register All_users ""
#不能为空 #strip ()
#用户名长度 Length 6-12 len ()
#最多输入3次 #循环

all_users = [' Zhang San ', ' John Doe ', ' WiFi ']
for I in range (3): username = input (' Please enter account: '). Strip ()
If Len (username) >5 and Len (username) <13:
if All_users.count (username) >0:
print (' That's a good name, it's already been registered! ')
Else:
Print (' Good name, register now! '
Break
Else:
Print (' username is not legal! ') Length between 6-12! ')
Else:
Print (' Excessive number of failures ')
 2. Dictionary {key:value} key-value pair 
Features:
1. Easy access to Data
2. Fast
3. The dictionary is unordered
4. If you loop a dictionary directly, the loop is the dictionary key

info={' name ': ' Fancy ', ' sex ': ' Female ', ' addr ': ' Earth '}
#查询:
Print (' phone ') ' does not take this key, return none
Print (Info.get (' phone ', 110)) does not take this key, the default is
print (info[' phone ') if the key does not exist error

#增加:
info[' phone ' ]= ' 13600000000 ' adds a key, and if key exists, it modifies the value of the original key pair (' age ': 32) To add a key, if key exists, The value

in the original key is not modified #修改:
info[' name ']= ' Lucy '

#删除:
Info.pop (' name ') deletes the key value pair for the specified key
Info.popitem () randomly delete a key
del info[' phone '] Delete the key value pair for the specified key
Info.clear () Clear dictionary

#字典常用方法 :
Print (Info.values ()) Gets the dictionary all value
Print (Info.keys ()) gets to the dictionary all key
Print (Info.items ()) Gets the dictionary many key value pairs, k-v

Note:
1. Loop through a dictionary with the dictionary key, such as:
for P in people:
Print (P)
2. When looping, take both key and value:
for k,v in People.items ():
Print (k,v)


Exercise:
Users = {
' Lucy ': ' 123456 ',
' Lili ': ' 456789 ',
}
#所有的账号和密码
# username
# pwd
# CPWD
# print (' 123456 ' in users) #字典里面用in来判断的话 just to determine if key exists
For I in range (3):
Username = input (' account: '). Strip ()
passwd = input (' Password: '). Strip ()
CPASSWD = input (' Password OK: '). Strip ()
If username== ' or passwd== ' or cpasswd== ':
Print (' username/password cannot be null ')
Elif Username in Users:
Print (' username ' is already registered! ‘)
Elif PASSWD!=CPASSWD:
Print (' Two input passwords are inconsistent ')
Else
Print (' Congratulations, registration successful! ‘)
# Users.setdefault (USERNAME,PASSWD)
users[username]=passwd
Break
Else
Print (' Excessive number of errors ')
Print (users)
3. Tuples: Tuples are also a list, and the difference between it and list is that the elements in the tuple cannot be modified
Similarly, loops, slices, small values
Note: If there is only one element in the tuple, you must add "," after this element, for example: t= (1,)
Print (T.index (2)) find the subscript of the element
Print (T.count (2)) find the number of elements
4. Commonly used string methods:
# a= '   string    \n\n\n\n\n '
# c = A.strip () #默认去掉字符串两边的空格和换行符
# c= A.lstrip () #默认去掉字符串左边的空格和换行符
# c = A.rstrip () #默认去掉字符串右边的空格
# print (' C ... ', c)
# print (' A ... ', a)

Words = ' http://www.baidu.com '
# Print (Words.strip (' du ')) #如果strip方法指定一个值的话, then these two values are removed
# Print (Words.count (' a ')) #统计字符串出现的次数
#print (Words.index (' z ')) #找下标, if the element is not found, it will be an error
# Print (Words.find (' z ')) #找下标, if the element is not found, return-1
# Print (Words.replace (' Bai ', ' Bai ') #替换字符串
# Print (Words.isdigit ()) #判断字符串是否为纯数字
# Print (Words.startswith (' http ')) #判断是否以某个字符串开头
# Print (Words.endswith ('. jpg ')) #判断是否以某个字符串结尾
# Print (Words.upper ()) #变成大写的
# Print (Words.lower ()) #变成小写的

Username = ' [email Protected]#¥ '
# Print (Username.isalpha ()) #判断字符串是否全为字母
# Print (Username.isalnum ()) #判断是否包含字母和数字, it's as long as there are letters or numbers return True

Python Learning note day3 (data type)

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.