[002]Introduction to Python Basics: Operator Precedence, Complex, Bin, Variables
Let’s talk about an important term when it comes to programming and that is operator precedents. What is that means? Well, we are using our knowledge from math class.
- The operator precedence means that the different operators have the precedence over different ones.
- We multiply the 3*4 first and get 12, then add the 12 to 20. This is because that the Python interpreter is going to follow a basic set of rules.
- When we click run, we get 32 because (3*4) gets evaluated first which equals to 12 and then add on to the 20.
The precedence is arrange in order:
- ( )
- **
- * /
- +-
- For instance, in this example, we found out that the (20–3) operation is performed before the 2**2 operation.
Extra data types: complex
- A complex number is a number which is a third type instead of int and float.
- Complex number will be only used if you are doing something that related to complex math.
What is Bin()?
There is an action or function that we can use in Python is called bin for binary.
For instance, the bin(10) will yield 0b1010. Instead of all zeros and ones, there’s also the idea of b. That is what Python used to notice that when a “0b” is present then that’s a binary number.
- Then, when I google for binary 10 and find out that the binary for 10 is 1010. The 1010 is what we seen before that appeared behind the 0b.
- We are also able to print the binary form in Python to the normal number that we human use. The 2 is used as the “0b1010” is a binary 2.
Variables:
- Variables is an important term for all programming language including Python as well. What exactly are the variables? Variables store information that can be used in our programs.
- For instance, we can hold user inputs like values when we log into Facebook. We may also hold some information such as our profile picture or date of birth as variable.
- Therefore, variables are ways for us to store information on our computer.
Let’s have a look on this:
- For instance, we take an iq quiz and found out that it is 190. However, we need to store that information somewhere. We can store the values using variables and then print it.
- Variables can also sometimes be referred as names. In addition, assigning a value is also known as binding. This is where the binding of the value to this variables/names. Thus, when later on we request for that variables, the machine will knows where to look for this information.
- Remember this number in memory gets stored as a binary representation in zeros and ones. However, this doesn’t matter to us because we just want to be able to retrieve it.
Best practices around variables (specific rules that the Python community agreed to):
Variables
- snake_case (means all lower case, spaces don’t exist and use underscores)
- Start with lowercase or underscore
- Letters, numbers, underscores
- Case sensitive
- Don’t overwrite keywords
For instance:
- user_iq have a underscores instead of a space. This practice make sure that a programmer (on a team) can read this variables.
- Start your variable in the lowercase in this case, you can also start it with underscores. (Ps. Now underscore in Python signifies a private variable.)
- We cannot access/retrieve the variable because it doesn’t exist. This is because Python is case sensitive and we have to be-careful with it.
- We get an error because we can’t really assigned this variable because print already means something. Technically, it overwrote the print() to now be an int(190). Thus, don’t overwrite the keywords.
- When we google the python keywords, we will discover that is a ton of them. If you don’t sure just google it and it is simply as that.
- As long as the variable that you type in is not highlighted in colour, then you are safe to use that variable. In addition, try to make the variable as descriptive as you can.
Variables can also be reassigned:
- In this example, we are able to reassigned the variables.
Idea of constants:
- Constants are those things that never change in a program. For instance the PI, we can have the PI all in capitals and this will tell other programmers that this is a constant.
- We can still changed and overrode it. We can still do that but a good convention is that if you see upper capitals variable means that this number or value should never be changed.
Quick tricks:
- This is a way for us to rapidly assign values to variables multiple times.
“Finally, variables are a very vital concepts and programming naming variables is important. Maybe this sounds silly. However, there is time where a code is so hard to understand as the programmer is not descriptive enough.”