How to play Python without data type?

Time:2022-1-12

give the thumbs-upcommentCollection = Look at you three times in a row

🍅 Contact authorDon’t eat tomatoes

🍅 Introduction to the author: CSDN blog experts – Top 8 of the whole station, members of HDZ core groupInformation technology think tankPublic number master

Resume template, PPT template, learning materials and technical assistance.Pay attention to me and give it to you

Many people read tomato blogs,garishI don’t know where to start. For the fundamental interests of the people, I decided to write a reading guide. Learn according to this guide and practice more. Over time, I’m bound to be able to… Forget it, it depends on how much you can learn and where you can stick to it. come on. Boy! after allCollection = Society

Learning path guidance (click to unlock) location Suitable population
The most complete Python quick start column in the whole network beginner Interested college students without language foundation
The darkest Python technology column in the whole network Black technology level Have a certain foundation and apply what you have learned
Big data knowledge system – highlights column Work / employment Practitioners in big data field, employment promotion and salary increase
Information technology think tank community Water blowing / mutual aid Internet related personnel, white whoring prizes – official activities

1、 What is the data type?

The data type is in Python system:Foundation in foundation, key in key

Data type solves the problem of “storage”: it determines the size of space required to use this type,What kind of posture is used to store in memory gracefully

Data types solve the problem of “fetching”: it changes the perspective of memory space andChoosing the appropriate data type in different code programs will not only have high program efficiency, but also get twice the result with half the effort in the development process

Next, I’ll explain Python data types in detail. I’ll skip some infrequent and difficult contents, and focus on presenting them to youPractical to explosiveOperation!!

Standard data type

There are six standard data types in Python 3:

  • Number

  • String (string)

  • List

  • Tuple (tuple)

  • Set

  • Dictionary

Among the six standard data types of Python 3:

  • Immutable data (3): number, string, tuple;

  • Variable data (3): list, dictionary, set.

2、 Number

Python 3 supports int, float, bool and complex (complex).

The type () method can determine the type of data.

1. Int type

>>> love_tomato = 10000
>>> print(type(love_tomato))
<class 'int'>

2. Float type

love_tomato = 10000.1111
print(type(love_tomato))
<class 'float'>

3. Bool type

love_tomato = True
print(type(love_tomato))
<class 'bool'>

Boolean types are: Yes or no, or 0 and 1

be careful: in Python 3, bool is a subclass of int. true and false can be added to numbers. True = = 1 and false = = 0 will return true, but the type can be determined by is.

4. Complex type (complex)

love_tomato = 4+3j
print(type(love_tomato))
<class 'complex'>

The complex number is composed of real part and imaginary part, which can be represented by a + BJ or complex (a, b). Both the real part a and imaginary part B of the complex number are floating-point

5. Numerical operation

>>>5 + 4 # addition
9
>>>4.3 - 2 # subtraction
2.3
>>>3 * 7 # multiplication
21
>>>2 / 4 # division to get a floating point number
0.5
>>>2 // 4 # division to obtain an integer
0
>>>17% 3 # remainder
2
>>>2 * * 5 # power
32

3、 String (string)

1. String assignment

Strings in Python are enclosed in single or double quotation marks.

Make a sample first:

#!/usr/bin/python3

str = 'love tomato'
STR2 = 'I love you, tomato' # Chinese string

2. String interception

#!/usr/bin/python3

str = 'tomato'

Print (STR) # output string
Print (STR [0: - 1]) # outputs all characters from the first to the penultimate
Print (STR [0]) # outputs the first character of the string
Print (STR [2:5]) # outputs characters from the third to the fifth
Print (STR [2:]) # outputs all characters starting from the third
Print (STR * 2) # outputs the string twice, or it can be written as print (2 * STR)
Print (STR + "I # love # U") # connection string

Output results:

tomato
tomat
t
mat
mato
tomatotomato
tomato, i Tm love u

be careful:

  • Strings can be concatenated with the + operator and repeated with the * operator.

  • Strings in Python can be indexed in two ways, starting with 0 from left to right and – 1 from right to left.

  • Strings in Python cannot be changed (for example, changing from wechat notomoto to tomato is not allowed).

4、 Dictionary

A dictionary is a mapping type. The dictionary is identified by {}. It is an unordered dictionaryKey: valueA collection of.

Different from other data types, the elements in the dictionary are accessed through the key, not through offset.

#!/usr/bin/python3

dict = {}
Dict ['one '] = "tomato cow"
Dict [2] = "I love you, tomato!"

Print (dict ['one ']) # outputs the value whose key is' one'
Print (dict [2]) # outputs the value of the key (2)
Print (dict) # outputs the complete dictionary
Print (dict. Keys()) # outputs all keys
Print (dict. Values()) # outputs all values

------------------------------------------

Output results:
Tomato beef
I love you tomato!
{'one': 'Tomato beef', 2: 'I love you tomato!'}
dict_keys(['one', 2])
dict_values(['Tomato beef', 'I love you tomato!'])

5、 Set

A set is composed of one or more elements and is out of order. What you see is the display order, and the actual storage is out of order. Therefore, the value cannot be obtained through the foot mark.

1. Create collection

Create a collection using braces {} or the set() function

Note: to create an empty collection, you must use set()

#!/usr/bin/python3

Sites = {'tomato', 'tomato2', 'smart', 'like', 'love', 'nature', 'ha ha'}

Print (sites) # outputs a collection, and duplicate elements are automatically removed

#Member test
if 'tomato' in sites :
    Print ('tomato in collection ')
else :
    Print ('tomato not in collection ')

---------------------------------------------------------------
{'love', 'ha ha', 'nature', 'tomato2', 'like', 'smart', 'tomato'}
Tomato # is in the collection

2. Set operation

#!/usr/bin/python3

A = {'tomato', 'smart', 'like', 'love', 'nature', 'ha ha'}
b = {'tomato', 'love_u', 'ture_2'}


print(a)

Difference set of print (a - b) # a # and # B #

Print (a | b) # union of a and B

Intersection of print (A & B) # a # and # B #

Elements in print (a ^ b) # a # and B # that do not exist at the same time

------------------------------------------------
{'tomato', 'love', 'nature', 'smart', 'hahaha', 'like'}
{'love', 'nature', 'ha ha', 'smart', 'like'}
{'ture_2', 'tomato', 'love', 'ture', 'smart', 'hahaha', 'love_u', 'like'}
{'tomato'}
{'nature_2', 'love', 'nature', 'smart', 'hahaha', 'love_', 'like'}

6、 List

1. List creation

List is the most frequently used data type in Python, and tomatoes are used almost every day.

The list is a comma separated list of elements written between square brackets [].

Like strings, lists can also be indexed and intercepted. After being intercepted, a new list containing the required elements is returned.

#!/usr/bin/python3

list = [ 'tomato', 123 , 2.34, 'weixin:notomoto', 10.2 ]
tinylist = [123, 'love']

Print (list) # outputs the complete list
Print (list [0]) # outputs the first element of the list
Print (list [1:3]) # starts from the second and outputs to the third element
Print (list [2:]) # outputs all elements starting from the third element
Print (tinylist * 2) # output the list twice
Print (list + tinylist) # connection list

------------------------Output results------------------------

['tomato', 123, 2.34, 'weixin:notomoto', 10.2]
tomato
[123, 2.34]
[2.34, 'weixin:notomoto', 10.2]
[123, 'love', 123, 'love']
['tomato', 123, 2.34, 'weixin:notomoto', 10.2, 123, 'love']

be careful:

  • List is written between square brackets, and elements are separated by commas.

  • Like strings, lists can be indexed and sliced.

  • Lists can be spliced using the + operator.

  • The elements in the list can be changed.

2. List function

List has many built-in methods, such as append (), clear (), and so on.

>>> list = [ 'tomato']
>>> list.append('love')
>>> print(list)
['tomato', 'love']

>>> list.clear()
>>> print(list)
[]

7、 Tuple (tuple)

Tuples are similar to lists, except that the elements of tuples cannot be modified. Tuples are written in parentheses (), and elements are separated by commas.

#!/usr/bin/python3

Tuple = ('tomato ', 123,' wechat: notomoto ')

Print (tuple) # outputs a complete tuple
Print (tuple [0]) # outputs the first element of the tuple
Print (tuple [1:3]) # output starts from the second element to the third element
Print (tuple [2:]) # outputs all elements starting from the third element

----------------------Output results----------------------
('tomato ', 123,' wechat: notomoto ')
tomato
(123, 'wechat: notomoto')
('wechat: notomoto',)

be careful:

  • Like strings, elements of tuples cannot be modified.

  • Tuples can also be indexed and sliced in the same way.

  • Tuples can also be spliced using the + operator.

End, weekly benefits

Lucky draw rules: 5 procedures per week, double prize pool system

Fan pool + prize pool. Put the participating fans and prizes every week in a pool and smoke together.

Prize list

At least 8 genuine books (plus 5 CSDN customized calendars and physical books this week), irregular: CSDN water cup, backpack, calendar

1. Participation mode

  • Sanlian articleIn this article, after three consecutive interactions, the crawler will collect your nameFan pool

  • Visit community: you can get points by commenting or posting in the community, and the top 50 people in the community scoreboard will enter automaticallyFan pool。 Community address: https://bbs.csdn.net/forums/ITID 

2. Winning noticeInformation technology think tank CSDN communityPython full stack technology group

3. Supplementary notes: the winning fan wechat provides the receiving address and the title of the book I want. Many people ask me which one to choose. You can choose it according to your interest and reference valueOfficial website priceAlso attached.

How to play Python without data type?

Book list: I can also choose the books I have sent before Official website address Official website price
Python 3. X basic tutorial https://item.jd.com/12542555.html 60
Python programming complete self-study tutorial https://item.jd.com/13130464.html 99
Introduction to Python to artificial intelligence https://item.jd.com/12648361.html 79
Python quantitative financial programming from entry to mastery https://item.jd.com/13045490.html 79
Python Network Programming from entry to mastery https://item.jd.com/12659295.html 69
Mathematical foundation of artificial intelligence and actual combat of Python machine learning https://item.jd.com/13504186.html 79

Theoretical basis of Python:The most complete Python quick start column in the whole network

Python practice application:The darkest Python technology column in the whole network

How to play Python without data type?