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