Python Syntax: An Overview of Indentation, Variables, and Comments

In this article, you will learn the basics of Python syntax, which include indentation, variables, and comments.

Python has gained immense popularity due to its easy-to-read syntax, making it a great language for both beginners and experts. If you’re new to Python, understanding its syntax is the first step towards becoming proficient in the language.



Python Syntax Execution

Python syntax can be executed in the Command Line, as we saw on the previous topic, by typing the following directly into the command line:

>>> print("This is your first code in python, Congrats.")

This is your first code in python, Congrats.

It can also be done manually by writing a Python script on the server, and then executing it directly from the command line, using the .py extension:

C:UsersYour Name>myfirstpythonfile.py

Python Indentations

Unlike many other programming languages, Python uses indentation to delimit blocks of code. This means that you need to be careful about how you indent your code, as it can affect the way your program runs.

Indentation in Python is typically four spaces or one tab. It’s important to be consistent with your indentation throughout your program.

Normally, the first space in a code line is called an indentation. For Example:

Example: 

if 4 < 8:print("Four is less than Eight!")

You will get an error if you do not indent the indentation in Python:

Example: 

if 4 < 8:print("Four is less than Eight!")

It is up to you as a programmer how many spaces you want, but at least one needs to be included.

Example: 

if 4 < 8:print("Four is less than Eight%21")if 10 < 5:print("Ten is less than Five%21")

Otherwise, Python will give you an error if you use inconsistent numbers of spaces in the same block of code:

Example: 

if 10 < 5:print("Ten is less than Five!")print("Ten is less than Five!")

Semicolon’s In Python:

Python allows statements to be terminated by semicolons ( ; ) .

Sometimes it is helpful to place a number of statements on a single line if there is a need for it.

see an example below:

lower = []; upper = []

Python allows the semicolon (;) to be used optionally to put two statements on a single line, similar to C but many Python style guides prohibit the insertion of semicolons when putting multiple statements on a single line.

As you can see there is no difference in output:

lower = []
upper = []

Variables In Python

Variables are used in Python to store values that you can later use in your program.

To create a variable in Python, you simply choose a name for the variable and assign it a value using the = sign. For example:

Example: 

x = 8 y = "My second variable."
Note: A variable cannot be declared in Python with a command.

During the Python syntax chapter, you will learn more about Python variables and how they work.


Comments In Python

Comments are used in Python to explain what your code does.

They’re not executed as part of your program, but they can be helpful for other developers who are reading your code.

In Python, you can create a comment by starting a line with the # symbol. For example:

Example: 

#This line is intended to be a comment line in python. print("I just learned how to comment in python. ")

This comment will not be executed as part of your program, but it can be helpful for explaining what your code does.


Conclusion

Python syntax is straightforward and easy to learn, even for beginners. By understanding the basics of indentation, variables, and comments, you’ll be able to start writing simple programs and exploring more advanced concepts. With practice and experience, you’ll become proficient in Python and be able to tackle more complex programming challenges.

 

Stay connected with the latest technical information on this site by subscribing to our Newsletter below.

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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