Python: Introduction, Setup and Fundamentals
Introduction: What is Python?
In 1989, while working at the National Research Institute of Mathematics and Computer Science in Netherlands, Guido van Rossum was looking to do something during his Christmas break. He decided to work on an interpreter for a new scripting language that he had thought about, which was based on ABC with a mix of Unix and C programming.
Version 2.0 was released in October of 2000 and included features such as garbage collector and a support for unicode.
Then, version 3.0 was released in December of 20This was a backwards-incompatible that improved on several features of the language. Since there was a wider support for libraries that ran on Python version 2.X, many developers refused to upgrade. However, currently, people are slowly beginning to move up to 3.0. This tutorial series will use 3.0.
Characteristics of Python
Here are some reasons why Pythonistas are going crazy over Python, and claiming it as the language of the future.
- Code readability is a top priority. Python's formatting is its syntax.
- Quick work cycle - no need to spend time compiling.
- Good for GUI application games, as well as file and text manipulations.
- Interactive - users are able to run and test programs from an interactive window.
- Large standard library with built-in functions.
- Portable - Compatible with UNIX, Mac OS X and Windows.
- Supports object-oriented imperatives.
- A very, very high level language that has support for functional programming.
- Dynamically typed system with commonly used data types built in.
- Automatic memory management (no need to allocate/deallocate memory for objects).
The Zen of Python - Python.org
Python Wikipedia Page - Wikipedia.org
Setting up Python
Head over the Python.org and download Python version 3.
To launch Python, simply type "python" onto your command line interface if you're on a Unix environment (Mac OS X or Linux). If you're on Windows, open Python's IDLE. You should see the version of Python 3.x.x, as shown below.
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information.
Python starts up in Interactive Mode. This means that you can type in some command and Python will interpret that query and output a response.
For example, try inputting a number. Python should output the number
>>> 123 123
Primary and Seconary prompts
Python lines will always be preceded by the primary prompt
>>>. If there is an unfinished command that goes to the next line, a secondary prompt will be activated with lines preceding with
>>> print("Hello world!" ... ) Hello world!
Notice how a secondary prompt appeared because the parentheses was not closed.
Comments in python are preceded by a hashtag (#) to the end of the line. As long as the hashtag is not embedded within quotes, it everything proceeding it will be ignored by the interpreter.
>>> # This is a comment that won't be interpretted. ... >>>
Zen of Python
Within your interpreter try typing
import this to print out the Zen of Python. Here you can find Python's philosophy.
>>> import this
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Finding help and keyboard shortcuts
Like any other command line utilities, Python contains methods for help. To get started with the help utility, type the
Welcome to Python 3.4's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://docs.python.org/3.4/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam".
Within this interactive module, you can type commands such as
Let's type in
q to close this interactive module and instead work directly from the Python command line.
Help on a certain function call
To get help on a specific function or method directly, use the
help() function, with a command inside the parentheses.
This will open up a help page that can be navigated by
less controls, as outlined below:
- Page down
- Page up
- Summary of more controller commands
Before we get started tinkering with Python, it's important to know how to quickly and efficiently navigate the keyboard. Let's learn some of the most important keyboard navigation shortcuts.
1) Beginning and Ends of lines
Use the Ctrl+A to move to the beginning of the line, and Ctrl+E to move to the end.
2) Moving among characters
Use Ctrl+B or the left arrow to move left one character, or Ctrl+F or the right arrow to move right.
3) Deleting characters
Ctrl+D will delete the next character, while Ctrl+K deletes the rest of the line after the cursor.
You may also use Ctrl+Y to "Yank" the last deleted text. This is essential cutting.
To undo, use Ctrl+_.
To go back and forth in history, use Ctrl+r and Ctrl+s respectively.
Try these commands out on your computer and practice them to save time in the future.
Expressions and Variables
An expression is some line of code that is executed to provide some meaningful result. Expressions can come in with unary, binary or ternary operators with several operands.
Unary expressions use just a single operand. For example, -1 is a unary expression since it only uses the negative (
>>> -1 -1 >>> +1 1
There is no need to use the
+ operand, as it's implicitly mentioned.
Binary expressions include one operator and two operands.
- Produces a float
- Floor division
- Power operators
- Modulo (remainder)
Try doing some math on your Python interpreter.
>>> 2 ** 5 32 >>> 123 % 23 8 >>> 19//2 9 >>> 23/2 11.5
= to assign a value to a variable. To output variable, simply type the variable name into the prompt.
>>> x = 3 >>> x 3
Function and Method Calls
Calls are a type of expression that call on some lines of code. A function call works by invoking a function, which is simply a code block with several lines of code.
Take, for example, the
print() function. As the help manual page says, this prints the values to a stream, or to sys.stdout (standard out) by default. Our standard out here, is the terminal window (or IDLE GUI on Windows). Thus, when we call the print command, it'll print out to our terminal window.
>>> print("Hello world!") Hello world!
Try out the
len() function as well, which is used to count the number of characters in some argument.
>>> len("Hello world!") 12
Additionally, there are functions that allow for user interaction, which we'll see soon.
>>> userName = input("What is your name? ") What is your name? JohnDoe
Now if you print out the
userName variable, you'll get the user's inputted name.
Furthermore, there exists method calls, which are functions that come with objects, or data types. To use a particular object's method, use the dot notation such as
For example, we can use method calls from the String object to run functions on any string we created.
This will look for how many times the G letter appears in our string. Note that this method is specific to the primitive data type string. If you try the same method call with an int, for instance, you'll get an error
>>> x = 42 >>> x.count("C") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute 'count'
We'll see many data types other than the string and int, and along with the variety of methods that come along with them.