Python 3: Lesson 3 - Basic Syntax & Rules
Python 3: Lesson 3 - Basic Syntax & Rules

By CN00b | Python | 18 Sep 2019


Hi people,

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

Example:

if True:
print("Hello")

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:

if True:
    print("Hello")

2. Python is case sensitive

Example:

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.

Examples:

- print("Hello Publish0x!")

- if True:

      print("something")

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


""" line
of
code """

The three quotes are used for larger texts with multiple lines.

5. Class Names starts with Uppercase 

Example:


class MyClass(): i defined a class, called MyClass

6. There are some identifiers or keywords that you cannot use, called "reserved words"

351665157-5e99ffcc7d44355fa4c143b36a44133927172820d0e6b3325a5cb25252a56bab.jpeg

(Source: Wikipedia)

7. Python allows you to put multiple statements in a single line, by using " ; " .

Example:


txt="Hello Publish0x";code=1234;name="CN00b"

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.

Example:


Yes: import os
import sys

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.

Cheers!


CN00b
CN00b

IT Hardware/ Software Noob Crypto Enthusiast Mountains Explorer


Python
Python

In this blog, i'll provide various lessons and tips for begginers and not only (later) about Python (version 3.xx), how to get started, how to use it, how to build an AI, etc. I'll try to cover more parts, but easy to understand, maybe 1-2 lessons/day.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.