Software Carpentry logo

Python Basics

April 24, 2010: We are pleased to announce that Version 4 of this course is now under development. For updates and an early peek at the content, please check out the Software Carpentry blog at http://www.software-carpentry.org/blog/.

1) Introduction

2) You Can Skim This Lecture If...

3) Python's Strengths

4) Python's Weaknesses

5) Why Another Language?

6) Execution Cycle

Sturdy vs. Nimble Execution

Figure 2.1: Sturdy vs. Nimble Execution

7) Running Python Programs

$ python
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print 124/28
4
>>> print 124.0/28.0
4.4285714285714288
>>> ^D
$ cat saved.py
print 124/28
print 124.0/28.0
$ python saved.py
4
4.42857142857

8) Execution Shortcuts

$ cat hashbang.py
#!/usr/bin/env python
print 124/28
print 124.0/28.0
$ hashbang.py
4
4.42857142857

9) Variables

planet = "Pluto"
moon = "Charon"
p = planet
Variables Refer to Values

Figure 2.2: Variables Refer to Values

planet = "Pluto"
moon = "Charon"
p = planet
planet = 9
Variables Are Untyped

Figure 2.3: Variables Are Untyped

10) Possible Mistakes

planet = "Sedna"
print plant      # note the misspelling
Traceback (most recent call last):
  File "lec/inc/py01/undefined_var.py", line 2, in ?
    print plant      # note the misspelling
NameError: name 'plant' is not defined

11) Who Has a Type?

x = "two"       # "two" is a string
y = 2           # 2 is an integer
print x * y     # multiplying a string concatenates it repeatedly
print x + y     # but you can't add an integer and a string
twotwo
Traceback (most recent call last):
  File "lec/inc/py01/add_int_str.py", line 4, in ?
    print x + y            # but you can't add an integer and a string
TypeError: cannot concatenate 'str' and 'int' objects

12) Printing

planet = "Pluto"
num_moons = 1
moon = "Charon"
print planet, "has", num_moons, "satellite",
print "and its name is", moon
Pluto has 1 satellite and its name is Charon

13) Quoting

print "He said, \"It ain't what you know, it's what you can.\""
He said, "It ain't what you know, it's what you can."
print "Sedna was discovered in 2004"
print 'It takes 10,500 years to circle the sun.'
print '''The tiny world may be part of the Oort Cloud,
a shell of icy proto-comets left over from
the formation of the Solar System.'''

14) Converting Values to Strings

print "Diameter: " + str(1280) + "-" + str(1760) + " km"
Diameter: 1280-1760 km
print int(12.3)
print float(4)
12
4.0

15) Escape Sequences

16) Numbers

17) Arithmetic

18) Booleans

19) Short-Circuit Evaluation

20) Comparisons

21) String Comparisons

22) Conditionals

a = 3
if a < 0:
    print 'less'
elif a == 0:
    print 'equal'
else:
    print 'greater'
greater

23) Why Indentation?

24) While Loops

num_moons = 3
while num_moons > 0:
    print num_moons
    num_moons -= 1
3
2
1
print 'before'
num_moons = -1
while num_moons > 0:
    print num_moons
    num_moons -=1
print 'after'
before
after
num_moons = 3
while num_moons > 0:
    print num_moons
    # oops --- forgot to subtract one
3
3
3

25) Break and Continue

num_moons = 3
while True:             # Looks like an infinite loop...
    print num_moons
    num_moons -= 1
    if num_moons <= 1:
        break           # ...but there's a way out
3
2
num_moons = 5
while num_moons > 0:
    print 'top:', num_moons
    num_moons -= 1
    if (num_moons % 2) == 0:
        continue
    print '...bottom:', num_moons
top: 5
top: 4
...bottom: 3
top: 3
top: 2
...bottom: 1
top: 1

26) String Formatting

27) Format Specifiers

28) Supported Formats

Format Meaning Example Output
"d" Signed decimal integer '%d %d' % (13, 15) "13 15"
"o" Unsigned octal (base-8) '%o %o' % (13, 15) "15 17"
"x" Lower case unsigned hexadecimal (base-16) '%x %x' % (13, 15) "d f"
"X" Upper case unsigned hexadecimal (base-16) '%X %X' % (13, 15) "D F"
"e" Lower case exponential floating point '%e' % 123.45 "1.234500e+02"
"E" Upper case exponential floating point '%E' % 123.45 "1.234500E+02"
"f" Decimal floating point '%f' % 123.45 "123.4500"
"s" String (converts other types using str()) '%s %s %s' % ('nickel', 28, 58.69) "nickel 28 58.69"

Table 2.6: String Formats in Python

29) Summary