Download presentation
Presentation is loading. Please wait.
Published byDelilah Anderson Modified over 9 years ago
1
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Python
2
Basics A simple interpreted language
3
PythonBasics A simple interpreted language no separate compilation step
4
PythonBasics $ python >>> A simple interpreted language no separate compilation step
5
PythonBasics $ python >>> print 1 + 2 3 >>> A simple interpreted language no separate compilation step
6
PythonBasics $ python >>> print 1 + 2 3 >>> print 'charles' + 'darwin' charlesdarwin >>> A simple interpreted language no separate compilation step
7
PythonBasics Put commands in a file and execute that
8
PythonBasics Put commands in a file and execute that $ nano very-simple.py
9
PythonBasics Put commands in a file and execute that $ nano very-simple.py print 1 + 2 print 'charles' + 'darwin'
10
PythonBasics Put commands in a file and execute that $ nano very-simple.py print 1 + 2 print 'charles' + 'darwin' $ python very-simple.py 3 charlesdarwin $
11
PythonBasics Use an integrated development environment (IDE)
12
PythonBasics Use an integrated development environment (IDE) Source file
13
PythonBasics Use an integrated development environment (IDE) Source file Execution shell
14
PythonBasics Variables are names for values
15
PythonBasics Variables are names for values Created by use
16
PythonBasics Variables are names for values Created by use: no declaration necessary
17
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>>
18
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>>
19
PythonBasics Variables are names for values Created by use: no declaration necessary variablevalue planet 'Pluto' >>> planet = 'Pluto' >>> print planet Pluto >>>
20
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> variablevalue planet moon 'Pluto' 'Charon'
21
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> p = planet >>> variablevalue planet moon 'Pluto' 'Charon'
22
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> p = planet >>> variablevalue planet moon p 'Pluto' 'Charon'
23
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> p = planet >>> print p Pluto >>> variablevalue planet moon p 'Pluto' 'Charon'
24
PythonBasics A variable is just a name
25
PythonBasics A variable is just a name Does not have a type
26
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>>
27
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet 'Pluto' variablevalue string
28
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue integer
29
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue Values are garbage collected
30
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue Values are garbage collected If nothing refers to data any longer, it can be recycled
31
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue Values are garbage collected If nothing refers to data any longer, it can be recycled
32
PythonBasics Must assign value to variable before using it
33
PythonBasics Must assign value to variable before using it >>> planet = 'Sedna' >>>
34
PythonBasics Must assign value to variable before using it >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling
35
PythonBasics Must assign value to variable before using it >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>
36
PythonBasics Must assign value to variable before using it Python does not assume default values for variables >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>
37
PythonBasics Must assign value to variable before using it Python does not assume default values for variables Doing so can mask many errors >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>
38
PythonBasics Must assign value to variable before using it Python does not assume default values for variables Doing so can mask many errors Anything from # to the end of the line is a comment >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>
39
PythonBasics Values do have types
40
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>>
41
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>>
42
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>> Would probably be safe here to produce 'two3'
43
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>> Would probably be safe here to produce 'two3' But then what should '2'+'3' be?
44
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>> Would probably be safe here to produce 'two3' But then what should '2'+'3' be? Doing too much is as bad as doing too little…
45
PythonBasics Use functions to convert between types
46
PythonBasics Use functions to convert between types >>> print int('2') + 3 5 >>>
47
PythonBasics Use functions to convert between types >>> print int('2') + 3 5 >>> print 2 + str(3) 23 >>>
48
PythonBasics Numbers
49
PythonBasics Numbers 14 32-bit integer (on most machines)
50
PythonBasics Numbers 14 32-bit integer (on most machines) 14.0 64-bit float (ditto)
51
PythonBasics Numbers 14 32-bit integer (on most machines) 14.0 64-bit float (ditto) 1+4j complex number (two 64-bit floats)
52
PythonBasics Numbers 14 32-bit integer (on most machines) 14.0 64-bit float (ditto) 1+4j complex number (two 64-bit floats) x.real, x.imagreal and imaginary parts of complex number
53
PythonBasics Arithmetic
54
PythonBasics Arithmetic Addition+35 + 2257
55
PythonBasics Arithmetic Addition+35 + 2257 'Py' + 'thon''Python'
56
PythonBasics Arithmetic Addition+35 + 2257 'Py' + 'thon''Python' Subtraction-35 - 2213
57
PythonBasics Arithmetic Addition+35 + 2257 'Py' + 'thon''Python' Subtraction-35 - 2213 Multiplication*3 * 26
58
PythonBasics Arithmetic Addition+35 + 2257 'Py' + 'thon''Python' Subtraction-35 - 2213 Multiplication*3 * 26 'Py' * 2'PyPy'
59
PythonBasics Arithmetic Addition+35 + 2257 'Py' + 'thon''Python' Subtraction-35 - 2213 Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / 21.5
60
PythonBasics Arithmetic Addition+35 + 2257 'Py' + 'thon''Python' Subtraction-35 - 2213 Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / 21.5 3 / 21
61
PythonBasics Arithmetic Addition+35 + 2257 'Py' + 'thon''Python' Subtraction-35 - 2213 Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / 21.5 3 / 21 Exponentiation**2 ** 0.51.41421356...
62
PythonBasics Arithmetic Addition+35 + 2257 'Py' + 'thon''Python' Subtraction-35 - 2213 Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / 21.5 3 / 21 Exponentiation**2 ** 0.51.41421356... Remainder%13 % 53
63
PythonBasics Prefer in-place forms of binary operators
64
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>>
65
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>>
66
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> The same as years = years + 1
67
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>>
68
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>> years %= 10 >>>
69
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>> years %= 10 >>> The same as years = years % 10
70
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>> years %= 10 >>> print years 5 >>>
71
PythonBasics Comparisons
72
PythonBasics Comparisons 3 < 5True
73
PythonBasics Comparisons 3 < 5True 3 != 5True
74
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False
75
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False Single = is assignment Double == is equality
76
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False
77
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False 1 < 3 < 5True
78
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False 1 < 3 < 5True 1 3True But please don't do this
79
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False 1 < 3 < 5True 1 3True 3+2j < 5error
80
October 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.