Before diving into Python programming, there are some rules that must be respected 100%, orelse you'll encounter a lot of errors, nerves, angry states and maybe, broken keyboards! :)
The rules are simple and basic:
1. Indentation - Python cares about whitespaces only at the beginning of the code. The standard is 4 spaces. Lucky for us, the console will print the errors name, so it will be easy for us to see where is the mistake. This a common one, not only a beginner encounteres it :P
This example will give an indentation error. Lucky for us, PyCharm for example, do this jos for us, format the code.
The correct version is:
2. Python is case sensitive
newobject is not the same as newObject
3. Python does not require semicolons at the end of statements. The only accepted format is mentioned at Point 7.
- print("Hello Publish0x!")
- if True:
4. Comments - If you write multiple lines of code and you don't want to forget what taht thing does or you just want to add a mention, that can be seen only by you - the intrepreter skips comments - you can add a comment.
There are 2 conventions used for comments:
# - used for a single line of code
The three quotes are used for larger texts with multiple lines.
5. Class Names starts with Uppercase
class MyClass(): i defined a class, called MyClass
6. There are some identifiers or keywords that you cannot use, called "reserved words"
7. Python allows you to put multiple statements in a single line, by using " ; " .
8. Import Modules
Modules are reusable files with pieces of code, that contains functions, classes, etc. Remember, Python is opne source, anyone can create modules, to be reusable for others.
When you import modules, always use separate lines, exception for submodules.
Yes: import os
No: import sys, os
These basic rules are to keep in mind when coding in Python. Are simple and you'll get used to it.
All of these said, it's to move to real programming in Python, starting with the next lesson.