- Python Basic Tutorial
That the latest version of python uses slightly di erent syntax than the previous versions. Also note that astronomy software may not work on Version 3; for the time being it is advisable to use Version 2.7) On Macs, a basic version of Python is already installed. Basic Python doesnt include many software packages, so we. Pandas Cheat Sheet for Data Science in Python A quick guide to the basics of the Python data analysis library Pandas, including code samples. The Pandas library is one of the most preferred tools for data scientists to do data manipulation and analysis, next to matplotlib for data visualization and NumPy, the fundamental library for scientific.
- Python Advanced Tutorial
- Python Useful Resources
- Selected Reading
The Python language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages.
First Python Program
Let us execute programs in different modes of programming.
Interactive Mode Programming
Invoking the interpreter without passing a script file as a parameter brings up the following prompt −
Type the following text at the Python prompt and press the Enter −
If you are running new version of Python, then you would need to use print statement with parenthesis as in print ('Hello, Python!');. However in Python version 2.4.3, this produces the following result −
Script Mode Programming
Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished. When the script is finished, the interpreter is no longer active.
Let us write a simple Python program in a script. Python files have extension .py. Type the following source code in a test.py file −
We assume that you have Python interpreter set in PATH variable. Now, try to run this program as follows −
This produces the following result −
Let us try another way to execute a Python script. Here is the modified test.py file −
We assume that you have Python interpreter available in /usr/bin directory. Now, try to run this program as follows −
This produces the following result −
Python Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.
Here are naming conventions for Python identifiers −
Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
Starting an identifier with a single leading underscore indicates that the identifier is private.
Starting an identifier with two leading underscores indicates a strongly private identifier.
If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.
Reserved Words
The following list shows the Python keywords. These are reserved words and you cannot use them as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.
and | exec | not |
assert | finally | or |
break | for | pass |
class | from | |
continue | global | raise |
def | if | return |
del | import | try |
elif | in | while |
else | is | with |
except | lambda | yield |

Lines and Indentation
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 −
However, the following block generates an error −
Thus, in Python all the continuous lines indented with same number of spaces would form a block. The following example has various statement blocks −
Note − Do not try to understand the logic at this point of time. Just make sure you understood various blocks even if they are without braces.
Multi-Line Statements
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 −
Statements contained within the [], {}, or () brackets do not need to use the line continuation character. For example −
Quotation in Python
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 −
Comments in Python
Python Syntax Cheat Sheet Pdf
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.
This produces the following result −
You can type a comment on the same line after a statement or expression −
You can comment multiple lines as follows −
Following triple-quoted string is also ignored by Python interpreter and can be used as a multiline comments:
Using Blank Lines
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.
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 −
Here, 'nn' is used to create two new lines before displaying the actual line. Once the user presses the key, the program ends. This is a nice trick to keep a console window open until the user is done with an application.
Multiple Statements on a Single Line
The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block. Here is a sample snip using the semicolon −
Multiple Statement Groups as Suites
A group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite.
Python Functions Cheat Sheet Pdf
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 −
Command Line Arguments
Many programs can be run to provide you with some basic information about how they should be run. Python enables you to do this with -h −
Python Syntax Cheat Sheet Pdf
You can also program your script in such a way that it should accept various options. Command Line Arguments is an advanced topic and should be studied a bit later once you have gone through rest of the Python concepts.
