Guido was trying to stay busy while his office was closed for Christmas
“eggs” and “spam” is commonly used instead of “foo” and “bar”
Example from Input and Output in the official Python tutorial
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni')) We are the knights who say "Ni!"
See which version of Python you’re running
$ python -V Python 2.7.12
Python 2:
#!/usr/bin/env python # This is a comment print "Hello, world"
Python 3:
#!/usr/bin/env python # This is a comment print("Hello, world")
Execute script
$ chmod +x first_script.py $ ./first_script.py Hello, world $ python first_script.py Hello, world
#!/usr/bin/env python
python
in $PATH#!/usr/bin/python
python
#!/usr/bin/python3.6
python
Enter Console
$ python Python 2.7.12 (default, Aug 9 2016, 15:48:18) [GCC 6.1.1 20160621 (Red Hat 6.1.1-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
exit()
or ctrl-d
>>> exit()
$
$ python -i first_script.py
Hello, world
Python 2:
>>> print "Hello, world" Hello, worldPython 3:
>>> print("Hello, world") Hello, world
print
is a statementprint()
is a functionTo use the print()
in Python 2, hop in a Delorean
>>> from __future__ import print_function >>> print("Hello, world") Hello, world
print("Hello, world")
produce the same output in Python 2 and 3?print(1, 2)
Standard indent is 4 space
No curly braces for code blocks
No semicolons at the end of statements
- Still used sometimes for one-liners
Indent to start a code block
Dedent to end a code block
Indents must match!
Notice the second set of dots? The interpreter is waiting for a dedent.
>>> for n in [1, 2, 3]: ... print n ... 1 2 3
Comments are preceded by a pound sign #
Comments can occur on separate lines or at the end of a line
# This is a comment print('something') # This is an inline comment
#!/usr/bin/env python
"""
This is where you would describe the script
It is also a good place to include contact and copyright information
"""
Appending a backslash \
to the end of a line will cause it to continue to the next line
Strings are automatically concatenated
>>> myString = "This is the string the never ends. " \ ... "It just goes on and on, my friend." >>> myString "This is the string the never ends. It just goes on and on, my friend."
Open parentheses, braces and brackets imply a line continuation
>>> print( ... 'something') something
urllib.request
is a module in the urllib
packageimport
statementImport a module
>>> import sys >>> sys.version_info sys.version_info(major=3, minor=5, micro=1, releaselevel='final', serial=0)
Import module with a different name
>>> import sys as system >>> system.version_info sys.version_info(major=3, minor=5, micro=1, releaselevel='final', serial=0)
version_info
>>> from sys import version, version_info
>>> version_info
sys.version_info(major=3, minor=5, micro=1, releaselevel='final', serial=0)
>>> from sys import *
The module search path is installation dependent
The current path can be displayed with sys.path
>>> sys.path ['', '/usr/lib64/python35.zip', '/usr/lib64/python3.5', '/usr/lib64/python3.5/plat-linux', '/usr/lib64/python3.5/lib-dynload', '/usr/lib64/python3.5/site-packages', '/usr/lib/python3.5/site-packages']
The path can be prepended with the PYTHONPATH
environment variable
$ PYTHONPATH=/usr/share/superpython:/usr/share/superduperpython python
help()
function, allows help access directly from the Python consoleIf the argument is a string, the topic will be searched for
>>> help('tuple')
>>> help(tuple)
>>> help(myTuple)
pydoc
command$ pydoc tuple
dir()
function