Discussion on python data Type and type conversion

  • 2020-06-15 09:45:16
  • OfStack

What are the core data types in Python?

Variables (Numbers, strings, tuples, lists, dictionaries)

What is data immutability? Which data types are immutable

The immutability of data is the index data, such as:


 a = ("abc",123) # Define a tuple 
 a[0]=234 # The first 1 A change to 345
 print(a) # An error will be reported when printing 

Immutable: number, character, tuple

Variable: lists and dictionaries

Common data types in Python

The assignment


counter = 100
miles = 1000
name = "nan"
print(counter,miles,name)

a = b = c = 1
a,b,c=1,2,"nan"

 There are two ways to assign values interchangeably: 1 Is the introduction of the first 3 A variable, 2 Is the following 
a,b = b,a

string


print(" String, a lot of strings ")
s = 'ilovepython'
print(s[0:5])
print(s[0:5] + 'nan')
print(s * 2)

The list of


print(" Lists, a lot of them ")
list = ['abcd',123,2.23,678,"nan"]
tinylist = [123,"nan"]
print(list)
print(list[0])
print(list[1:3])
print(list[2:])
print(tinylist*2)
print(list+tinylist) # On the 1 A list of 

list = ["jia",3764]
list[1] = 'lala'
print(list)

 Convert a list to a string: 
list = ''.join(list)
print(type(list)) # Now it's shown as str type  

 How to make abcde String reversed into edcba ? 
str = "abcde"
print(str[::-1])

tuples


 print(" Primitives are similar to lists except that they are denoted by () and cannot be performed 2 Once the assignment ")
tuple = ("abcd",123,3.45)
# tuple[1]= 'nan'  In tuples, it's invalid 

The dictionary


print(" The dictionary ")
dict = {'name':'nan','code':798,}
print(dict.keys())
print(dict.values())
print(dict.get("name","not found"))
print(dict.get("namet","not found")) # Look it up in the dictionary key for namet If you don't have this key print  not found; If you have key The value prints the corresponding value

Common data type conversions


'''
int()
str()
list()
dict()
'''
a = 100
print('nan'+str(a))
stre = '1111'
print(int(stre))
'''
print(type(len(stre))) #len is int type 
print(" Print the length of the "+str(len(stre)))
'''

Take the specified element of list below


L = [
  ['GOOGLE', 'APPLE', 'LINUX'],
  ['Python', 'Java', 'PHP', 'C'],
  ['Jack', 'Tom', 'Peter']
]
print(L[0][0],L[1][1],L[2][2])
''' Print results: GOOGLE Java Peter'''

conclusion

The above is all about python data types and type conversion, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: