Home / Blog / Basic Python Syntax | First Program in Python
python syntax

Basic Python Syntax | First Program in Python

Simple Print Code using Python

Here is a simple code to show you how Python is simple and easy to learn for a person who has no programming knowledge. The user can execute Python syntax by writing directly in the Command Line.

Basic Python Syntax

Code

print("Hello, Python!")
Python
Hello, Python!

Indentation

Python indentation shows that when a block of code begins and ends. In Python indentation is very important its not just a simple spacing for the readability of code it’s a predefined set of rules for spacing of code If you write a code without indentation it will generate an error. Other programming languages use brackets or keywords instead. Let’s use an example to understand indentation.

print("""
{
  "Hello, Python"
}
""")
Python
Output:
{
“Hello, Python”
}

As we can see in the above example print is indented four spaces.

Comments in Python:

Comments are very useful lines to explain any code line to others. It is a good practice for a developer to add comments to the code. There the two types of comments single-line comments and multiple-line comments.

Single-line comments can be used by writing # as you can see in the following example:

#This is a single-line comment
Python

Multiple-line comments can be written by using “”””      ””” triple quotes as you can see in the following example:

"""
This is a multiline comment.
It can span multiple lines without causing any syntax errors.
You can use either single quotes or double quotes for multiline comments.
"""
Python

Comment lines will be ignored by the Python interpreter.

Example:

Code

# Print Hello Python
print("Hello Python")
Python
Output:
Hello Python

Reserved Words:

The reserve has a specific use you can not use the theme as a variable name, object name, or any constant or identifier name. Every language has its own reserved words also known as keywords. The following list shows Python reserved.

Python Keywords

Andexecnot
assertfinallyor
breakforpass
classfromprint
continueglobalraise
defifreturn
delimporttry
elifinwhile
elseiswith
exceptlambdayield

Data Types in Python

In every programming language, there will be nothing done without data and every piece of data has its type there are different types of data. In Python, we have some built-in data types for different types of data as:

Built-in Data TypesTypes of Data
strText Type
int, float, complexNumeric Type
list, tuple, rangeSequence Type
dictMapping Type
Set, frozensetSet Type
boolBoolean Type
Bytes, bytearray, memoryviewBinary Type

We can check the type of data by using the type () function:

Example:

Print the data type of the variable x:

Code

X = 3
print(type(X))
Python
Output:
<class 'int'>

Escape Sequence

Escape sequences are control characters used to move the cursor and print such as ‘, “, \, etc.

Escape Sequence  Meaning
\aBell
\bBackspace
\fForm feed
\nNewline
\rCarriage Return
\tHorizontal Tab
\vVertical Tab
\newlineBackslash and Newline ignored
\\Backslash
\’Single Quote
\”Double Quote

Author: TCF Editorial
Copyright The Cloudflare.

Post navigation