Before you start writing your first Python program, you’ve got to learn the basics. We will walk you through Python syntax basics that will help as a building block for your Python career.
To get started, let’s first write a very basic Python program.
There are two ways in which you can write and execute a basic Python program:
Let’s see those in action.
To enter the interactive Python mode enter the following command on your Terminal.
C:\Users\Zigya>python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
And, now you should be in the interactive mode.
But, if you are using an IDE you don’t need to type the command above to get into interactive programming mode.
Here is Basic Python syntax for executing a Hello World Program.
When you write this program and press enter, your IDE should display “Hello, Word!”
print("Hello World”)
Anything that you write within “ “ of print(“ ”) gets printed on your IDE.
In our case, the output that you should see on your screen would be
Hello, World!
When you wrote your Hello World program earlier, let’s assume that you saved it in a Python file. When you save your first program, look for app .py extension file.
Assuming that you saved it as Hello_Python.py, here’s how you would execute this code in a scripting mode.
First of all, the file you saved has to be made executable. We ideally do that by the command:
$ chmod +x test.py
Now that your file is executable, let’s run the program in scripting mode.
$ python Hello_Python.py
Once you execute this command, you will see “Hello, World!” was printed on your Terminal.
"Hello, World”
And, there you have learned your first Python Syntax.
You can directly write python along with the file you want to execute
C:\Users\Zigya>python Hello_Python.py
Once you execute this command, you will see “Hello, World!” was printed on your Terminal.
"Hello, World”
And, there you have learned your first Python Syntax.
There are a total of 31 keywords and 5 major identifiers that you should know in Python.
A Python identifier usually is a variable, a function, a class, a module or it can be any other object. You give a name to an entity in Python, it is called an identifier. A valid identifier starts with a letter(A-Z, a-z both apply) or an underscore(_) which can be followed by zero, letters, underscores or numbers(0-9).
Types of Identifiers in Python:
Let’s go over and check what Keywords are available in Python
To see all 3 Python Keywords, open your IDE and type the following:
>>>import keyword
>>> keyword.kwlist
You should see something like this as the output
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
These keywords may often change as new Python versions are released. A couple things in mind:
These Keywords are also often referenced as Reserved words.
Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.
The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example −
if True: print "True" else: print "False"
Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example −
total = item_one + \ item_two + \ item_three
Statements contained within the [], {}, or () brackets do not need to use the line continuation character. For example −
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
Python accepts single (‘), double (“) and triple (”’ or “””) quotes to denote string literals, as long as the same type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines. For example, all the following are legal −
word = 'word' sentence = "This is a sentence." paragraph = """This is a paragraph. It is made up of multiple lines and sentences."""
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them.
# First comment print "Hello, Python!" # second comment
This produces the following result −
Hello, Python!
You can type a comment on the same line after a statement or expression −
name = "zigya" # This is again comment
You can comment multiple lines as follows −
# This is a comment. # This is a comment, too. # This is a comment, too. # I said that already.
Following triple-quoted string is also ignored by Python interpreter and can be used as a multiline comments:
''' This is a multiline comment. '''
A line containing only whitespace, possibly with a comment, is known as a blank line and Python totally ignores it.
In an interactive interpreter session, you must enter an empty physical line to terminate a multiline statement.
Conclusion
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science
Keep Reading! Cheers!
Zigya Academy
BEING RELEVANT
Controlling immunological responses requires the cytokine interleukin-2 (IL-2), commonly referred to as IL-2 Human. It…
What is Mortgage Insurance? Mortgage insurance is a type of insurance policy designed to protect…
Through the standard form offers different advantages in mathematical calculations and scientific notation. Firstly, it…
Introduction Stress is a feeling caused by an external trigger that makes us frustrated, such…
Sociology is a broad discipline that examines societal issues. It looks at the meaningful patterns…
Some info about Inch Inches are a unique measure that persuades us that even the…