Writing Python syntax rules


Writing Rule Syntax

After preparing all the equipment for coding python and know how to make a python program, then let us learn about the rules of writing Python syntax that must be obeyed.
What if no obeyed? The risks can occur error when running / compile the program.
Now let’s discuss some of the basic rules of writing Python syntax must be known.

Statement Writing Python

Statement is an instruction or a command line to be executed by the computer.
Example coding programs:
1print(“Hello World!”)
2print(“Belajar Python”)
3nama = “Fajar Yusuf”
Writing a statement does not end with a semicolon. Whereas, if we want to write more than one statement in one line, then we must separate it with a semicolon.
Example coding programs:
1nama_depan = “Fajar”; nama_belakang = “Yusuf”
2print(“Hello World”); print(“Tutorial Python”)
3print nama_depan, nama_belakang;
According to some python your style, not recommended to write more than one statement in a single line. Because it would be difficult to read.

Writing Strings in Python

  • String is a text or set of characters.
  • String in programming usually written wrapped in quotation marks.
  • Can use single or double quotation marks.
Example coding programs:
1judul = “Belajar Pemrograman Python sampai Bisa”
2penulis = ‘Fajar Yusuf’
3print judul;
4print penulis;
Or we can also use triple quotes.
Example coding programs:
1judul = “””Belajar Python di”””
2penulis = ”’Fajar Yusuf”’
3print judul;
4print penulis;

Writing Case in Python

Python syntax is case sensitive, meaning that thistext with Thisttext distinguished.
Example coding programs:
1judul = “Belajar Dasa-dasar Python”
2Judul = “Belajar Membuat Program Python”
3print judul;
4print Judul;
Among the variables will be distinguished title by title

Case Style

According to Google’s recommendation your style, here’s an example of writing a case is recommended:

## Snake Case used on: module_name, package_name, method_name, function_name,, global_var_name, instance_var_name, function_parameter_name, local_var_name.

## camelCase used On: className, ExceptionName


Block writing Python Program

Block program is a collection from multiple statements that are combined in one block.
Writing program block must be added indentation (tabs or spaces 2x / 4x).

block Program

Examples of the correct coding block:

# Block branching if
if username == ‘fajaryusuf’:
print ( “Welcome Students”)
print ( “Please take a seat ‘)
# block branching for
for i in range (10):
print i

Examples of block coding is wrong:

# Block branching if
if username == ‘fajaryusuf’:
print ( “Welcome Students”)
print ( “Please take a seat ‘)
# block branching for
for i in range (10):
print i

There are several kinds of program blocks:
  • block Branching
  • blocks Recurrence
  • block Functions
  • block Class
  • block Exception
  • blocks With

How Writing Comments in Python

  • Comments are lines of code that will not be executed.
  • Comments are used to provide additional information and to disable the code.
  • There are several ways to write a comment on Python programming.

Using Hash (#)

The first method uses a hash mark (#).
This method is most often used.

# This is a comment
# This is also a comment

Using Quotation Mark

In addition to wrapping (string), quotation marks can also be used to make a comment.
Example coding programs:

“This is the comment by double quotes”
‘This is also a comment, but with single quotation marks’

Writing comments in quotation marks are rarely used, most people prefer to use a hash mark. So … not recommended.
Using Triple Quote
While triple quotation marks, often used to write documentation.

Fences class:
“” A “class fence fence to create objects. Created by FajarYusuf an example.” “”
def __init __ (self, color, height, material):
self.warna = color
self.tinggi = high
self.bahan = material
# class documentation Accessing
print Fences doc__ .__
raw_input ( ‘ nPress [enter] to see help (documentation) class:’)
help (Fences) # to view the documentation kelas_

The result:

$ Python kelas_pagar.py
class to create objects fence railing.
created by FajarYusuf
as an example.

press [enter] to see help (documentation) class:
After Enter pressed

Help on class Fence in the main module:
class Fence
| fencing class to create objects fence.
| created by FajarYusuf
| as an example only.
| Methods defined here:
| __init __ (self, color, height, material)

Leave a Reply

Your email address will not be published. Required fields are marked *