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.
int
and long
, but this distinction is not significant for Python, so in Python 3.x there's only one type of integer: int). It supports binary (e.g., 0b100
, which is 4 in decimal), octal (e.g., 0o100
, which is 64 in decimal), decimal (100
), and hexadecimal (e.g., 0x100
, which is 256 in decimal) representations.123.456
) and scientific notation (e.g., 1.23456e2
).'hello'
or "hello"
. Python also supports raw string notation, byte string notation, Unicode string notation, and multi-line strings (enclosed in triple single quotes or triple double quotes).True
or False
. In Python, you can directly use True
and False
to represent boolean values (note the capitalization), or they can result from boolean operations (e.g., 3 < 5
produces the boolean value True
, while 2 == 1
produces False
).3+5j
, similar to mathematical notation except that 'j' is used instead of 'i' for the imaginary part. This type is not frequently used; it's good to be aware of it, but don't worry too much about it.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.
a
and lowercase A
are two different variables).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.
int()
: Converts a number or string to an integer, with an optional base specification.