Python căn bản (2): Syntax

[Từ điển]

Bài trước: Python căn bản (1): Introduction

-----

2. Python Syntax

2.1 Execute Python Syntax

As we learned in the previous page, Python syntax can be executed by

creating a python file on the computer, using the .py file extension,

and running it in the Command Line:

C:\Users\Your Name>python myfile.py

2.2 Python Indentation

Indentation refers to the spaces at the beginning of a code line.

Wherein other programming languages the indentation in code is for

readability only, the indentation in Python is very important.

Python uses indentation to indicate a block of code.

Example:

if 5 > 2:

  print("Five is greater than two!")

Python will give you an error if you skip the indentation:

Example

Syntax error:

if 5 > 2:

print("Five is greater than two!")

The number of spaces is up to you as a programmer,

the most common use is four, but it has to be at least one.

if 5 > 2:

 print("Five is greater than two!")

if 5 > 2:

        print("Five is greater than two!")

You have to use the same number of spaces in the same block of code,

otherwise Python will give you an error:

Example

Syntax error:

if 5 > 2:

 print("Five is greater than two!")

        print("Five is greater than two!")

2.3 Python Comments

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.

Creating a comment

Comments starts with a #, and Python will ignore them:

Example

#This is a comment

print("Hello, World!")

Comments can be placed at the middle of a line, and Python will

ignore the rest of the line:

Example

print("Hello, World!") #This is a comment

A comment does not have to be text that explains the code,

it can also be used to prevent Python from executing code:

Example

#print("Hello, World!")

print("Cheers, Mate!")

Multiline comments

Python does not really have a syntax for multiline comments.

To add a multiline comment you could insert a # for each line:

Example

#This is a comment

#written in

#more than just one line

print("Hello, World!")

Or, not quite as intended, you can use a multiline string.

Since Python will ignore string literals that are not assigned to a variable,

you can add a multiline string (triple quotes) in your code,

and place your comment inside it:

Example

"""

This is a comment

written in

more than just one line

"""

print("Hello, World!")

As long as the string is not assigned to a variable, Python will read the code,

but then ignore it, and you have made a multiline comment.

2.4 Exercise

1. Which character is used to define a Python comment:

A. ///

B. #

C. //

D. /*

2. True or False: Indentation in Python is for readability only.

A. True

B. False

3. Insert the missing part of the code below to output "Hello World".

....("Hello World")

A. print 

B. console.log

C. printer

D. cout

-----

Bài sau: Python căn bản (3): Variables

-----

Bạn muốn học Python căn bản tại Đà Lạt, liên hệ