Python programming | introduction to Python - DTU
[Pages:56]Python programming -- introduction to Python
Finn ?Arup Nielsen
DTU Compute Technical University of Denmark
September 9, 2013
Python programming -- introduction
Content
Invoking Python A basic program Datatypes, sequences, control structures, functions, object-orientation File processing, Exception, Generators Libraries Documentation, Testing, checking coding style with pylint
Finn ?Arup Nielsen
1
September 9, 2013
Python programming -- introduction
Invoking python . . .
From the command line with no argument and interactive:
$ python >>> 1+1 With the file mypythonscript.py with the following content
print(1+1) From the command line with a python function:
$ python mypythonscript.py From the command line with a python function:
$ python >>> import mypythonscript
Finn ?Arup Nielsen
2
September 9, 2013
Python programming -- introduction
. . . Invoking python . . .
With a shell-like program myscript
#!/usr/bin/python print(1+1)
Executing the script as a standard (UNIX) program
$ chmod u+x myscript $ ./myscript
Or execute it from within Python
>>> import os >>> os.system('myscript')
Finn ?Arup Nielsen
3
September 9, 2013
Python programming -- introduction
. . . Invoking python . . .
Construct a string with the Python code for execution
s = 'a = 1+1; print(a)' exec(s)
and evaluation
s = '1+1' a = eval(s) print(a)
or a script
execfile('myscript.py')
Finn ?Arup Nielsen
4
September 9, 2013
Python programming -- introduction
. . . Invoking python
mymodule.py with the following content
def myfunction(): print(1+1)
def myotherfunction(): print(2+2)
Load the library and call the functions in the library:
$ python >>> import mymodule >>> mymodule.myfunction() >>> mymodule.myotherfunction()
Finn ?Arup Nielsen
5
September 9, 2013
Python programming -- introduction
Invoking python: IPython
"An Enhanced Interactive Python" with automatic completion and some more help.
$ ipython
In [1]: a = 1 + 1
In [2]: ?a
Type:
int
Base Class:
String Form: 2
Namespace:
Interactive
Docstring:
int(x[, base]) -> integer
Finn ?Arup Nielsen
6
September 9, 2013
Python programming -- introduction
A python program
import math, sys
# Importing modules.
def formatresult(res):
# Define function. Remember colon!
"""This is the documentation for a function."""
return "The result is %f" % res # Percentage for formating
if len(sys.argv) < 3:
# Conditionals should be indended
print("Too few input argument")
elif len(sys.argv) > 10: # Not 'elsif' or 'elseif'
print("Too many input argument")
else:
res = 0;
# Semicolon not necessary. Considered bad style
for n in range(1, len(sys.argv)): # Not first element in loop
try: res += float(sys.argv[n]) # One-liner: no identation
except: pass # One-liner!
print(formatresult(res))
Finn ?Arup Nielsen
7
September 9, 2013
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python programming introduction to python dtu
- using the command line stanford university
- statistical software for data science stata
- advanced python excercises with solutions
- pdb commands stanford university
- command line arguments2 colorado state university
- pandas datareader documentation read the docs
- advanced functions powerful python
- reading headers and calling functions donald bren school of
- hyperopt a python library for optimizing the hyperparameters of
Related searches
- introduction to java programming pdf
- introduction to java programming and data structures
- introduction to java programming 10th
- introduction to java programming liang
- introduction to java programming ppt
- python programming books free pdf
- best python programming book
- python programming language pdf book
- free python programming books
- python programming pdf free download
- introduction to python pdf
- python programming tutorials