Basic syntax in python
In the previous article, we have seen how to write a simple hello world program. Before going into more details lets first go through python identifiers, reserved keywords and other important basic syntax rules.
Python Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other objects. An identifier starts with a letter [A-Z,a-z] or an underscore (_) followed by zero or more letters, digits [0-9] or underscores.
- To declare a private identifier it should start with a single leading underscore.
-
To declare a strongly private identifier it should start with two leading
underscore . - Class names start with an uppercase letter. Others start with a lowercase letter.
- If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.
Reserved Words
Like in every language, python also have a list of reserved keywords. These reserved words cannot be used as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only. Below is the list of python reserved keywords.
and | assert | break | class | continue | def | del |
| else | except |
exec | finally | for | from | global | if | import | in | is | lambda |
not | or | pass | raise | return | try | while | with | yield |
Lines and Indentation
Unlike in other languages, in python blocks of code are denoted by line indentation. Python does not provides braces to indicate blocks of code. The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount.
Thus, in Python all the continuous lines indented with the same number of spaces would form a block as shown in below
if True: print "True" else: print "False"
Multi-Line Statements
Statements in Python typically end with a new line. In order to have multiline statement in Python one has to use the line continuation character (\) to denote that the line should continue as shown in example.
total = item_one + \ item_two + \ item_three
Statements contained within the [], {}, or () brackets do not need to use the line continuation character as shown in below.
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
Quotation in Python
To denote a string literals python allows use of single ('), double (") and triple (''' or """ ) quotes, as long as the same type of quote starts and ends the string.
word = 'word with single quote to denote string’ sentence = "This is a sentence with double quote to denote string" paragraph = """This is a paragraph. It is made up of multiple lines and sentences with triple quote to denote string"""
Multiple Statements on a Single Line
The semicolon ( ; ) allows multiple statements on the single line
import sys; x = 'foo'; sys.stdout.write(x + '\n')
Multiple Statement Groups as Suites
A single code block having multiple statements are called suites in Python.
Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are followed by one or more lines which make up the suite. For example −
if expression : suite1 elif expression : suite 2 else : suite3
Comments in Python
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.
#!/usr/bin/python # First comment print "Hello, World!” # second comment
Using Blank Lines
Python totally ignores blank lines(A line containing only whitespace, possibly with a comment).
Waiting for the User
The following line of the program displays the prompt, the statement saying “Press the enter key to exit”, and waits for the user to take action.
#!/usr/bin/python raw_input("Press the enter key to proceed.”)
That’s it for now :). We will explore more of it as we learn more in next articles.
That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.
We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.
About the Author
Tags: python, Python Tutorials
Comments and Queries
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.