Difference between revisions of "Python"
(Updated the description to be compatible with Python 3.) |
|||
Line 56: | Line 56: | ||
<source lang="python"> | <source lang="python"> | ||
− | print "Hello World" | + | print("Hello World") |
fruits = ["apple", "banana", "cherry", "date"] | fruits = ["apple", "banana", "cherry", "date"] | ||
for fruit in fruits: | for fruit in fruits: | ||
− | print "I always love to eat a fresh %s." % fruit | + | print("I always love to eat a fresh %s." % fruit) |
# Map the fruits list over to a new list containing the length of the fruit strings: | # Map the fruits list over to a new list containing the length of the fruit strings: | ||
Line 66: | Line 66: | ||
avg_fruit_size = sum(fruit_size) / float(len(fruit_size)) | avg_fruit_size = sum(fruit_size) / float(len(fruit_size)) | ||
− | print "The average fruit string length is %4.2f." % avg_fruit_size | + | print("The average fruit string length is %4.2f." % avg_fruit_size) |
</source> | </source> | ||
Some things to notice: | Some things to notice: | ||
− | * Printing is achieved using the '''print''' | + | * Printing is achieved using the '''print''' function. |
* A colon starts a block, similar to a curly brace '''{''' in many other languages. The corresponding code block '''''must''''' be indented. The end of the code block is signified by when the indentation ends. | * A colon starts a block, similar to a curly brace '''{''' in many other languages. The corresponding code block '''''must''''' be indented. The end of the code block is signified by when the indentation ends. | ||
* Strings can be printf-style formatted using the '''%''' operator | * Strings can be printf-style formatted using the '''%''' operator | ||
− | * | + | * Comments start with a pound symbol '''#''' |
* We can transform/map a list to a new list in just one line. (Beat that, Java!) | * We can transform/map a list to a new list in just one line. (Beat that, Java!) | ||
− | * When we compute the average fruit size, we need to cast ''len(fruit_size)'', which returns an int, to a float in order to prevent integer truncation | + | * When we compute the average fruit size, we need to cast ''len(fruit_size)'', which returns an int, to a float in order to prevent integer truncation. |
For some more examples, see [http://wiki.python.org/moin/SimplePrograms the Python wiki]. | For some more examples, see [http://wiki.python.org/moin/SimplePrograms the Python wiki]. | ||
Line 84: | Line 84: | ||
<source lang="python"> | <source lang="python"> | ||
− | def | + | def hello(name): |
− | print "Hello, %s!" % name | + | print("Hello, %s!" % name) |
− | + | hello("Batman") | |
− | + | hello("Superman") | |
+ | </source> | ||
+ | |||
+ | === Lists === | ||
+ | Unlike languages like Java and C, Python's array-like type doesn't have fixed length. Instead, a '''list''' is a dynamic array of objects of any type. Here's an example: | ||
+ | |||
+ | <source lang="python"> | ||
+ | pets = ['Dog', 'Cat', 'Fish'] # Lists can be creating by placing their items between square brackets | ||
+ | |||
+ | random_items = ['Apple', 12312, 2.0, [1, 2, 3]] # Lists can contain objects of different types - even other lists! | ||
+ | |||
+ | pets.append('Turtle') # Adds 'Turtle' to the end of pets | ||
</source> | </source> | ||
=== Tuples === | === Tuples === | ||
− | Python has a special datatype called ''' | + | Python has a special datatype called '''tuple''', which is an unmodifiable array. |
<source lang="python"> | <source lang="python"> | ||
− | first_name, last_name = | + | cities = ('St. Louis', 'Los Angeles', 'Seattle') # Tuples are defined using parentheses. |
+ | single_item_tuple = (1,) # To make a tuple with only one item, put a comma after it | ||
+ | </source> | ||
+ | |||
+ | |||
+ | They can also serve as convenient ways to assign multiple variables at once: | ||
+ | |||
+ | <source lang="python"> | ||
+ | first_name, last_name = "John", "Smith" | ||
</source> | </source> | ||
Line 119: | Line 138: | ||
=== Dictionaries === | === Dictionaries === | ||
− | Python has another datatype called ''' | + | Python has another datatype called a '''dictionary''' (or '''dict''', for short), which are like ''maps'' in Java, ''associative arrays'' in PHP, and ''object literals'' in JavaScript (coming up soon in Module 6). Essentially, they enable you to use any immutable object as the key in your data structure. |
<source lang="python"> | <source lang="python"> | ||
Line 130: | Line 149: | ||
for fruit, num in fruits_in_bowl.items(): | for fruit, num in fruits_in_bowl.items(): | ||
− | print "There are %d %s(s) in the bowl." % (num, fruit) | + | print("There are %d %s(s) in the bowl." % (num, fruit)) |
+ | </source> | ||
+ | |||
+ | If you only want the keys from a dictionary, you can just iterate over it directly, like this: | ||
+ | <source lang='python'> | ||
+ | for fruit in fruits_in_bowl: | ||
+ | print(fruit) | ||
</source> | </source> | ||
Line 137: | Line 162: | ||
=== Sorting === | === Sorting === | ||
− | Sorting in Python is frequently performed using the '''sorted''' function, which takes two arguments: a | + | Sorting in Python is frequently performed using the '''sorted''' function, which takes two arguments: an iterable (anything you can do a for-loop over, including lists and tuples) and a function used to evaluate each item. If the function is omitted, Python will try to sort the lists by value. |
The following example demonstrates using an inline function, which Python calls a ''lambda''. | The following example demonstrates using an inline function, which Python calls a ''lambda''. | ||
Line 145: | Line 170: | ||
# sort the fruits by string length | # sort the fruits by string length | ||
− | new_fruits = sorted(fruits, key=lambda v: len(v) ) | + | new_fruits = sorted(fruits, key=lambda v: len(v) // 2) |
+ | |||
+ | print(new_fruits) # ['date', 'apple', 'banana', 'cherry'] | ||
+ | </source> | ||
+ | |||
+ | Alternatively, you can sort a '''list'' using the '''.sort''' method. This is faster than calling '''sorted''', but it modifies the list in-place. | ||
+ | |||
+ | <source lang="python"> | ||
+ | fruits = ['apple', 'banana', 'cherry', 'date'] | ||
− | |||
</source> | </source> | ||
Line 182: | Line 214: | ||
f.close() # free up memory when we're finished with the file | f.close() # free up memory when we're finished with the file | ||
</source> | </source> | ||
+ | |||
+ | A better option is to use a with-block, which handles opening and closing the file for you automatically: | ||
+ | |||
+ | <source lang="python"> | ||
+ | with open("example.txt") as f: | ||
+ | file_contents = f.read() | ||
+ | </source> | ||
+ | |||
You can read a file line-by-line like this: | You can read a file line-by-line like this: | ||
<source lang="python"> | <source lang="python"> | ||
− | + | with open("example.txt") as f: | |
− | for line in f: | + | for line in f: |
− | print "Read line: %s" % line.rstrip() | + | print("Read line: %s" % line.rstrip()) |
− | |||
</source> | </source> | ||
Line 196: | Line 235: | ||
<source lang="python"> | <source lang="python"> | ||
− | |||
− | |||
with open("example.txt", "w") as f: | with open("example.txt", "w") as f: | ||
f.write("Hello\nWorld\n") | f.write("Hello\nWorld\n") |
Revision as of 00:03, 2 February 2017
Languages like Java and C++ have lots of rules regarding variable types, syntax, return values, and so on. Although these restrictions help make the compiled program run quickly, they are cumbersome when you are trying to write short, quick scripts to perform tasks. This is where a scripting language comes into play.
Python is a language well-suited to rapid prototype development. It is an interpreted language, which means that you do not need to compile the code when you run it. The syntax is clean, and it is usually clear at first glance what is going on when you write in Python.
Installation
Python may already be installed on your system. To see whether or not it is, enter the command
$ python --version
If it tells you a version of Python (like "2.7.1"), then you're good to go. If not, you need to do a quick package install to get it up and running. Apt and Yum both call a functional Python package python.
Pip
Linux distributions have package managers like Apt, Yum, and YaST. PHP has a package manager named PEAR. It's now time to introduce Python's leading package manager: pip.
You need to install Pip from Apt or Yum before you can use it. Both call the package python-pip.
Once you have pip installed, you can use it to install Python packages. Use the pip-python (RHEL) or pip (Debian) command:
$ pip-python install package_name # RHEL
$ pip install package_name # Debian
Running Python
There are two common ways to run Python code: via the console, and via a Python script file.
The Python Console
The Python console enables you to experiment with code without opening a text editor. To enter the Python console, simply type the python command at the terminal:
$ python
To leave the interactive console, either type "quit()" or press Ctrl-D (on both Mac and Windows).
Python Script Files
You can also save Python script files for later use. The extension for Python scripts is *.py. To run a script file, simply feed its path as an argument to the python command in Terminal:
$ python my_script.py
Python Syntax and Language Components
This section contains a very brief overview of Python syntax. For a more comprehensive introduction, see the Python docs.
An Example Python Script
In my mind, there's no better way to learn Python than to be immersed in a simple example script.
print("Hello World")
fruits = ["apple", "banana", "cherry", "date"]
for fruit in fruits:
print("I always love to eat a fresh %s." % fruit)
# Map the fruits list over to a new list containing the length of the fruit strings:
fruit_size = [len(fruit) for fruit in fruits]
avg_fruit_size = sum(fruit_size) / float(len(fruit_size))
print("The average fruit string length is %4.2f." % avg_fruit_size)
Some things to notice:
- Printing is achieved using the print function.
- A colon starts a block, similar to a curly brace { in many other languages. The corresponding code block must be indented. The end of the code block is signified by when the indentation ends.
- Strings can be printf-style formatted using the % operator
- Comments start with a pound symbol #
- We can transform/map a list to a new list in just one line. (Beat that, Java!)
- When we compute the average fruit size, we need to cast len(fruit_size), which returns an int, to a float in order to prevent integer truncation.
For some more examples, see the Python wiki.
Functions
Define functions using the def keyword:
def hello(name):
print("Hello, %s!" % name)
hello("Batman")
hello("Superman")
Lists
Unlike languages like Java and C, Python's array-like type doesn't have fixed length. Instead, a list is a dynamic array of objects of any type. Here's an example:
pets = ['Dog', 'Cat', 'Fish'] # Lists can be creating by placing their items between square brackets
random_items = ['Apple', 12312, 2.0, [1, 2, 3]] # Lists can contain objects of different types - even other lists!
pets.append('Turtle') # Adds 'Turtle' to the end of pets
Tuples
Python has a special datatype called tuple, which is an unmodifiable array.
cities = ('St. Louis', 'Los Angeles', 'Seattle') # Tuples are defined using parentheses.
single_item_tuple = (1,) # To make a tuple with only one item, put a comma after it
They can also serve as convenient ways to assign multiple variables at once:
first_name, last_name = "John", "Smith"
Tuples also enable you to have multiple return values from a function:
def compute_length(string):
str_len = len(string)
if str_len < 5:
return (str_len, "short")
elif str_len < 40:
return (str_len, "medium")
else:
return (str_len, "long")
length, description = compute_length("Four score and seven years ago")
print "The %s string is %d characters long." % (description, length)
The above example also demonstrates Python's if...elif...else conditional structure.
Dictionaries
Python has another datatype called a dictionary (or dict, for short), which are like maps in Java, associative arrays in PHP, and object literals in JavaScript (coming up soon in Module 6). Essentially, they enable you to use any immutable object as the key in your data structure.
fruits_in_bowl = {
'apple': 4,
'banana': 2,
'cherry': 0,
'date': 12
}
for fruit, num in fruits_in_bowl.items():
print("There are %d %s(s) in the bowl." % (num, fruit))
If you only want the keys from a dictionary, you can just iterate over it directly, like this:
for fruit in fruits_in_bowl:
print(fruit)
If you want only the values but not the keys from a dictionary, use my_dictionary.values().
Sorting
Sorting in Python is frequently performed using the sorted function, which takes two arguments: an iterable (anything you can do a for-loop over, including lists and tuples) and a function used to evaluate each item. If the function is omitted, Python will try to sort the lists by value.
The following example demonstrates using an inline function, which Python calls a lambda.
fruits = ['apple', 'banana', 'cherry', 'date']
# sort the fruits by string length
new_fruits = sorted(fruits, key=lambda v: len(v) // 2)
print(new_fruits) # ['date', 'apple', 'banana', 'cherry']
Alternatively, you can sort a list using the .sort' method. This is faster than calling sorted, but it modifies the list in-place.
fruits = ['apple', 'banana', 'cherry', 'date']
Import
If you want to use functions from other libraries (including ones that you install using pip), use import:
import time
current_time = time.localtime()
print time.strftime('%a, %d %b %Y %H:%M:%S', current_time)
If you want to pull the functions out of their namespace, you can use from ___ import ___ syntax:
from time import localtime, strftime
current_time = localtime()
print strftime('%a, %d %b %Y %H:%M:%S', current_time)
# Be aware that this technique, although convenient, may cause unexpected behavior if the function names that you're pulling out of the namespace are already used for other purposes in Python.
File I/O
You can read an entire file into a variable like this:
f = open("example.txt")
file_contents = f.read()
f.close() # free up memory when we're finished with the file
A better option is to use a with-block, which handles opening and closing the file for you automatically:
with open("example.txt") as f:
file_contents = f.read()
You can read a file line-by-line like this:
with open("example.txt") as f:
for line in f:
print("Read line: %s" % line.rstrip())
You can write to a file like this:
with open("example.txt", "w") as f:
f.write("Hello\nWorld\n")
Command-Line Arguments
Command line arguments are accessible in the variable sys.argv.
The following example shows a program that expects a filename as its argument, and it prints a usage message if the argument is not present (source).
import sys, os
if len(sys.argv) < 2:
sys.exit("Usage: %s filename" % sys.argv[0])
filename = sys.argv[1]
if not os.path.exists(filename):
sys.exit("Error: File '%s' not found" % sys.argv[1])
Object-Oriented Programming
You can define and use a class like this:
class Food:
# constructor:
def __init__(self, name):
self.name = name
@staticmethod
def get_definition():
return "Food is nourishment for carbon-based lifeforms."
def format_name(self):
return "Gotta love to eat " + self.name
class Fruit(Food):
def format_name(self):
return Food.format_name(self) + " (fruit)"
fruit = Fruit("Cherry")
print fruit.format_name()
print Food.get_definition()
This is the same example as in the PHP guide.
Some things to notice:
- Static methods require the @staticmethod decorator
- Non-static methods always take self as their first argument, followed by any number of additional parameters. This can be misleading for programmers familiar with other languages, because the number of arguments you feed to the method is actually one less than the number of declared parameters. Whenever you call a method on a class instance, that instance is implicitly fed into the explicitly-declared self parameter of the method.
- Note that the self variable has the same purpose as the this variable in languages like PHP, Java, JavaScript, and C++.
- Take home message is that you need to add an additional parameter, self, at the beginning of any instance method.
- There is no need for a new keyword in Python.