In this post, you’re gonna learn some basic features of Python, or actually every program languages.
Values and Types
There is no mystery in the word ‘values’, it is what it means in English. Value is the basic part that Python works with, such as adding 2 values, or multiplying or diving or something.
Every value belongs to one type. I’m going to give a definition for each type. You just look the examples below and you will understand.
- Integer: 2, 16, 999,…
- Float: 26.08, 1.6, 0.999,…
- String: ‘SonCo’, ‘KhThu’, ‘KTSC’,…
There are more types other than the above, but you don’t need to mind it right now. And you don’t need to remember either. You can ask Python anytime, using
>>> a = '16' >>> type(a) <class 'int'> >>> b = 'SCKT' >>> type(b) <class 'str'>
In Python’s eyes, 999,999,999 looks like 3 numbers (999 and 999 and 999). So please be careful.
Variables and Assignment Statements
A variable is a name that refers to a value.
So you got a value. This value might be long, or hard to remember or whatever that you don’t want to call it in full. So you give that value a name, like a word, make it represent for your value. That name is called variable.
The act of giving a value a name is called assignment.
In Python, you use
= to assign.
>>> n = 'Anh Son Thui' >>> print(n) 'Anh Son Thui'
You must follow some rules when naming a variable.
- Variable names can contain numbers, letters and underscores.
- Variable names can’t begin with numbers.
- Variable names can’t be one of the reserved words. Below is the list of reserved words
These word will be displayed in different color so you don’t need to remember all of them.
In case you cannot live without using these words, you can take advantage of one of Python’s characteristic: case-sensitive. That means, ‘False’ can’t be a variable name, but ‘false’ can.
But, you know, you can do it doesn’t mean you should do it. Who wants to name a variable ‘And’ anyway?
Expressions and Statements
An expression is a combination of values, variables, and operators.
A statement is a unit of code that the Python interpreter can execute.
– Extracted from Python for Everybody, Charles R. Severance
Simply speaking, a statement is a unit of code that produces something, that has an effect, like assigning a variable.
When your program gets bigger and bigger and contains more and more lines of code, it becomes difficult to read. For this reason, adding comments is needed to explain what the program is doing.
# Assigning a value to a variable a = 123 b = 456 # Adding 2 values c = a + b
You’re not adding comments only for others to read. You add comments for the sake of yourself, too.
You are getting some fundamental elements to build your coding knowledge. In next post, you’re gonna try to write you first program.