How to calculate different types?

Introductory tutorials, case source code, learning materials, readership

Please visit:  python666.cn

Hello everyone, welcome to Crossin's programming classroom!

In Python, every piece of data has a definite type. The most common data types are:

  • str - the string

  • int - an integer

  • float - floating point number, that is, decimal

  • bool - Boolean type, a type used to represent the two logical states of true and false

There are also compound data types, such as:

  • list - the list

  • dict - the dictionary

  • tuple - tuple

Although some data "look" similar, if the type is different, the operations that can be performed are also different.

For example, the input obtained through input is all str string type.

Even if the user enters a number, such as 3, but the program gets a string 3 instead of the number 3, it cannot be directly added to the number 1, otherwise an error will be reported.

faa89c5bcd51ded1af3cd96fb24e3e59.png

If you want to perform mathematical operations on the obtained value, the common solution is to do a type conversion to let the program know that it is a number.

a = input()
a = int(a)
a = a + 1
print(a)

Although the data type increases the restriction, it allows the program to match the data with the appropriate operation, reducing the possibility of program error.

Even if some languages ​​do not perform mandatory type checking, the program still needs to store data in a certain format. For example, the character 3 and the number 3 are different data for the computer and cannot be stored in the same way. It's just that these languages ​​​​will try to convert it into a usable type when using it. This is more flexible during development, but it also hides risks.

A cold knowledge: A well-known small website crashed for 3 hours before because of the misuse of a string 0 and a number 0. (Lua language does not perform mandatory type checks. When b is the character '0' or nan, the program can run normally, but it will not enter if return, so it will fall into an infinite loop)

e652d7a08349906824f01bd96176c29d.png

In addition, it is not that different types must not be combined for calculations. Some calculations are performed between specific types. For example, strings and numbers can be multiplied:

print('3' * 2)

result:

33

What else do you want to know about types in Python? Welcome to discuss in the message area.


The following is the video time, welcome everyone to pay attention, like, and forward:


Crossin's second book " Operation on Code: Using Python and ChatGPT to Efficiently Get Excel Data Analysis " is now on the market.

67d8f37e05ba9ca7c96f657067f09f0c.jpeg

This book explains the ideas, methods and practical applications of processing and analyzing data from the perspective of the combined use of Python and Excel. Whether you are a learner who wants to engage in data analysis or an office worker in other occupations, you can master the skills of Python to analyze data through the study of this book. The book innovatively introduces ChatGPT into teaching, uses ChatGPT to answer questions and provides practical training codes, and introduces some practical skills of using ChatGPT to assist learning, bringing a new way of learning to learners.

Readers and friends of the official account can contact me in the background after purchase and join the reader exchange group. Crossin will open the accompanying reading mode for you and answer all your questions when reading this book.

Thank you for retweeting and liking ~


_Previous article recommendation_

Why do you need input?


If you want to learn about paid quality courses and teaching Q&A services

Please reply in Crossin's programming classroom : 666

35daa6f208e632d34291b81918cf6b23.jpeg

Guess you like

Origin blog.csdn.net/qq_40523737/article/details/132095220