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:
1 print("Hello World!")
2 print("Belajar Python")
3 nama = "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:
1 nama_depan = "Fajar"; nama_belakang = "Yusuf"
2 print("Hello World"); print("Tutorial Python")
3 print 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:
1 judul = "Belajar Pemrograman Python sampai Bisa"
2 penulis = 'Fajar Yusuf'
3 print judul;
4 print penulis;
Or we can also use triple quotes.
Example coding programs:
1 judul = """Belajar Python di"""
2 penulis = '''Fajar Yusuf'''
3 print judul;
4 print penulis;

Writing Case in Python

Python syntax is case sensitive, meaning that thistext with Thisttext distinguished.
Example coding programs:
1 judul = "Belajar Dasa-dasar Python"
2 Judul = "Belajar Membuat Program Python"
3 print judul;
4 print 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)

Spread the love

Fajar (Ucup)

The owner and author of FajarYusuf.com and also someone who likes to share interesting information with others.

Leave a Reply

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