Python, what is it good for?
Python, first released in 1991, is a general purpose language emphasizing easy readability and logical structure and is meant to be fun to use.
Some advantages of Python:
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Readability counts. –from The Zen of Python
Java and C++ are object-oriented languages. Python is a multi-paradigm language. This means it has several different ways it is generally used, including object-oriented modes.
Let’s talk about:
There are 2 main branches of Python. Annoying!
Over time, languages are refined and improved. Python 2 was released in 2000. Python 3 was a large revision of the language and released in 2008. So many ongoing programs were written in 2 that a decision was made to continue to improve Python 2 even while Python 3 was further developed. Python 2.7 will be sunset on January 1, 2020, meaning that the language will not be further developed, de-bugged or supported. When you find code online, check to see whether it is in Python 2 or Python 3.
Generally, most shell’s run Python 2 by default. To launch the Python 3 interpreter, type python3
if it’s installed.
print("hello world")
NOTE: In Python2, you can also just do print "hello world"
.
Python uses indentation to specify blocks of code, instead of { }. The amount of indentation indicates your current scope level.
example:
if x == 1:
print "x is 1"
Python is a dynamic language. You do NOT need to declare the type of variable. You do not need to declare a variable before using it.
myInt = 7
myFloat = 7.0
myString = “some text”
Lists are similar to arrays in other languages. Python has a number of built in methods to work with lists. Lists may contain any mixture of strings and numbers and variables.
myList = [1, 2, "three"]
myList.append('another thing')
myList.insert(0,"zero")
del myList[4]
myList.pop() # removes last item
myList.pop(4) # removes 4th item
myList.remove('zero') #removes a value that matches
myList.sort() #sort
myList.sort(reverse=True) #sort in reverse
myList.reverse() #flip list around
len(myList) # the length of the list
for item in myList:
print(item)
for value in range(1,6):
print(value)
digits = [0, 1, 2, 3]
min(digits) #0
max(digits) #3
sum(digits) #6
print(digits[0:3]) # slicing
newList = digits[] #points to old list
digits.append(4)
print(newList) #0, 1, 2, 3, 4
#copying list just points to old list!
tuple = (0, 1, 2)
tuple[0] = 23 # ERROR!
dictionaries are combinatons of key-value pairs.
alien = {'color':'green','age':5}
def hello(username):
print("hello "+username)
hello("Xavier")
def hi(username="Lee"):
print("hi "+username)
hi() # prints hi Lee, the default parameter
hi("Justine") #prints hi Justine
Modules are files with variables and functions and classes. You can import them into your program to use them.
There are a number of prebuilt modules, or you can download others’ modules.
import pizza #imports a file pizza.py
pizza.make_pizza('pepperoni')
# runs the make_pizza function, with a parameter, from pizza module
# OR
#
from pizza import make_piza
#imports just a single function instead of entire module
# OR create an alias for a function
from pizza import make_pizza as mp
mp('pepperoni')
from pizza import * #imports everything
my nates on altering PirateBox on Pi
sudo nano /etc/minidlna.conf
The completed Piratebox project should have: