Programming is not about magic, it's about modeling (or formalizing) our real world. Programmers are trying to solve real world problems by coding in the virtual digital world, and the first step to do this is modeling data.
The most basic data type is character, which is used for conveying messages. By concatenating characters, we then have a string, such as “I am learning programming” and “我在学编程”, which consist of English and Chinese characters respectively. In Python, a string is enclosed in single quotes or double quotes.
# comments in Python start with the hash character '#'
# function type() is used to determine the type of data
a_string = 'a string 一个字符串'
a string 一个字符串 <class 'str'>
Besides the human writable characters, a string can hold non-printable characters, and emojis as well.
# \n is newline character, it starts a new line
# function len() is used to count the number of characters
another_str = "Can 🐶 play ⚽️ ?\n🤔 I am thinking about it."
print('There are', len(another_str), 'characters in the string.')
Can 🐶 play ⚽️ ?
🤔 I am thinking about it.
There are 41 characters in the string.
In fact, strings are unicode by default in Python 3. Unicode defines a codespace of 1,114,112 code points in the range 0 to 10FFFF (in hex format), which is far more than the total number of characters used on our planet, thus we also use it for other things such as lovely emojis. (see Unicode and UTF-8)
Another fundamental data type is number. In Python, there are two types of numbers: integer (natural number, e.g., 100) and float (real number with a decimal point, e.g. 10.1).
x = 100
y = -0.1
z = x*y
100 <class 'int'>
-10.0 <class 'float'>
The equal sign '=' means 'assignment' in programming, i.e., to assign the value (or the data) of right side to the left side. We call the left side 'x', 'y', 'z', variables, coz the value of a variable can change. A variable has three element: name, value and type.
The variable name is just a label to distinguish it with others. The value is the data that variable points to, and the type is the type of data it refers to. For example, 'x' is the variable name, 100 is the value, and the data type is integer.
var_a = 'a string variable'
var_a = 100
a string variable <class 'str'>
100 <class 'int'>
Python is a loosely typed language, which means that the data type of a variable can also change, and we don't need to declare data type for variables. This is different from a strong typed language such as C/C++. In C, we first declare a data type for a variable, and then the usage is confined to that data type. For example, if we define an integer i, and assigns 10 to its value, we cannot add 0.1 to a itself to make it become a float 10.1. Since 10.1 is a float, we need to declare a float first.
int i = 10;
i = 0.1 + i;//this is wrong!!!
j = 0.1 + i;//this is correct
The third primitive data type is Bool, consisting of only two possible values, true and false. Boolean type is used to making decision based on some conditions.
a = False
# is 1 + 1 equals to 2?
a = 1+1==2
print('1+1==2 is', a, type(a))
False <class 'bool'>
1+1==2 is True <class 'bool'>
Since the equal sign '=' is used for assignment, we have to use double equal sign '==' for condition checking. Note that the right side of '=' is evaluated first before the assignment, i.e., 1+1==2 is evaluated to True and then the value True assigned to variable a.