Python Basic Rules #1

erizky ningtya
4 min readMar 30, 2021

Python Program Code Structure

Python is a very minimalist programming language
Examples of Hello World code like this are:

print("Hello World")

Statement Terminator

Python uses the new line character as a command separator, technically speaking. This newline character is a line break marker created by pressing the Enter key.

print("Hello World")print("Learning Python")print("Don't give up!!")

If each different command line is enough, there is no need to close (;). But if we mark (;) Python will not protest. If we want to write several commands in one line, then writing the closing (;) is required.

print("Hello World"); print("Learning Python");print("Don't give up!!");

But writing like this is not recommended, because it will be difficult for us to read it. It is better if only one command per line.

Difference in upper / lower case in Python

In python, there are rules for writing the character case. The Python language has case sensitive writing rules, which means that upper and lower case letters are considered different. Such as the print command cannot be written to Print or PRINT. This applies to other commands such as variables and keywords.

Indentations

Indentation is shifting or “indenting” several lines of code inward. This is used to make it easier to read program code, but in Python, indentation functions as a program code block marker. And is the rule of writing pep8.

And a result like this will appear :

The purpose of this rule is, for example, if the print command is cleared, it will cause an error. Like this :

We will discuss more indentation and program code blocks when entering the if and looping conditions. For now, what you need to understand is, the space at the beginning of the program code is very important in Python.

Comments

These comments are like notes or notes that are usually used to write the intent of the code. Comments are not processed by the Python interpreter.

For large projects involving many programmers, comments will make it easier for other programmers to understand the meaning of the code we are writing.

To create a comment in Python, start a line with a hash or pound sign (#), like the following example:

An easy way to comment lines is ctrl + /

Variable Creation

In Python, variables can be written directly when they are used. To assign a value to a variable, use the sign (=), like the following example:

Displays variables

With result :

Change the variable value

The value of a variable can be overwritten by any other value. Variables in Python are not limited to one type only. A variable can be filled with various data types, ranging from text (string), numbers (number), and various other data types. Here’s the experiment:

With result :

Throughout the above program code, the contents of the menu variables have changed from string (text), number (number) and boolean (True / False) data types. All of this can be done in the Python programming language.

Variable naming rules

Variables can consist of letters, numbers, and underscore characters (_). The first character of a variable can only be letters and an underscore (_) cannot be numbered. However, variables that start with an underscore character can have a special meaning in Python.
Variables must be other than keywords. For example, we cannot use the word continue as a variable name, because continue is a keyword or a special command in Python.

Technically, a variable naming rule refers to an identifier naming rule. An identifier is a name for the “name of something” written by the programmer. And the rules above do not only apply to variable names but, also to function names, class names, object names which all include identifiers.

Constants in python

The Python language does not recognize constants, which are variables whose values cannot be changed throughout the program code.

To solve this, the Python programmer’s agreement is to capitalize the variable name to represent a constant, like the following program code:

This creates nothing but 3 variables. It’s just that because this variable name is written in all caps, most Python programmers will think of it as a constant.

--

--