First Python Course

Variables and Data Types

In programming, variables are containers for storing data. In a computer, variables are actual data or memory spaces in the storage that hold data. The values of variables can be read and modified, which forms the basis of all computation and control. Computers can process many types of data, including not just numbers but also text, graphics, audio, video, and various other forms of data. Different types of data require different storage types. Python has many data types and also allows us to define new ones (which we'll discuss later). Let's introduce some commonly used data types.

Variable Naming

For each variable, we need to give it a name, just as each of us has our own distinctive name. In Python, variable naming needs to follow these mandatory rules and strongly recommended guidelines.

Of course, as a professional programmer, it's also very important to make variable names (in fact, all identifiers) self-explanatory.

""" Storing data in variables and performing arithmetic operations """


a = 321
b = 12
print(a + b)    # 333
print(a - b)    # 309
print(a * b)    # 3852
print(a / b)    # 26.75

https://github.com/ateliershen/Python-100-Days-zh_TW/blob/master/Day01-15/02.语言元素.md

In Python, you can use the type function to check the type of a variable. The concept of functions in programming is consistent with the concept of functions in mathematics, which should be familiar to most people. It includes the function name, independent variables, and dependent variables. If you don't understand this concept right away, don't worry. We will specifically explain the definition and use of functions in subsequent chapters.

a = 100
b = 12.345
c = 1 + 5j
d = 'hello, world'
e = True
print(type(a))    # <class 'int'>
print(type(b))    # <class 'float'>
print(type(c))    # <class 'complex'>
print(type(d))    # <class 'str'>
print(type(e))    # <class 'bool'>

You can use Python's built-in functions to convert variable types.