Re-study Python-Day 04-python Foundation - > tuple + dictionary + string learning

Keywords: Python Java Ruby shell

tuple

 

Tuple: The feature is that the content is immutable. It is a read-only list that can be queried and not modified.

a = 2,
print(a)
print(type(a))

b = ('a','b','c')
print(b[1])

Operation results:

(2,)
<class 'tuple'>
b

 

Dictionaries

 

Dictionary: The internal data is printed out in disorder, the key value is hashed, and has uniqueness.

#Create dictionary
a = {'a':'python','b':'ruby','c':'java'}  #Create a dictionary directly
print(a)

#Built-in dictionary method creation: dict
a = dict((('a','b'),))
print(a)

a = dict([['a','b'],])
print(a)

#Modify dictionary
a = {'a': 'b'}
set1 = a.setdefault("c","java")
print(a)
print(set1)

#Query operation
a = {'a':'python','b':'ruby','c':'java'}
print(a.keys())
print(a.values())
print(a.items())
print(a['a'])

#update Method
a = {'a':'python','b':'ruby','c':'java'}
b = {'d':'shell'}
a.update(b)
print(a)
a.update({'a':'javascript'})
print(a)

#Deletion method
a = {'a':'python','b':'ruby','c':'java'}
a.clear()
print(a) # Empty dictionary

a = {'a':'python','b':'ruby','c':'java'}
del a['a']
print(a)

print("********************")
a = {'a':'python','b':'ruby','c':'java'}
b = a.pop('b')
print(a)
print(b)

print("********************")
a = {'a':'python','b':'ruby','c':'java'}
b = a.popitem()
print(a)
print(b)

a = {'a':'python','b':'ruby','c':'java'}
del a  #The whole dictionary was deleted

print('Other dictionary methods')
a = {'first':'python'}
b = dict.fromkeys(['a','b'],['test'])
print(a)
print(b)

print('Traversal of Dictionaries')
a = {'a':'python','b':'ruby','c':'java'}
for i,j in a.items():
    print(i,j)

for i in a:    #Efficiency is higher than above
    print(i,a[i])

 

Character string

List only a few simple applications

print('join String merging method')
a = ['python','ruby','java']
b = ''.join(a)
print(b)

c = '*'.join(a)
print(c)


print('Common String Methods')
a = 'python py'
print(a.count('p'))
print(a.capitalize())

print("format Format output")
a = "my favorite book {name} and {name2}"
print(a.format_map({'name':'python','name2':'java'}))
print(a.format(name = 'shell',name2 = 'bash'))
print(a.isdigit())

 

 

PS:

Previous data types are only the basis, master the commonly used built-in functions, basic grammar can be, in learning functions, objects, some advanced topics, can continue to consolidate, so continue to refuel it.

Today, when I watch python at work, I am despised by the development of c++. It's too simple to say. Alas, go ahead.

Posted by dhaselho on Thu, 07 Feb 2019 09:30:17 -0800