Python intro


class notes [pdf]

python tutorial

notes
need to use escape characters for some elements
if you are using single quotes for something other than delimiting the string, it needs to be marked as unique — not a command

>>> 'I\'m in graduate school'

length = takes an argument and returns the length of the string

>>> len("hello")
5

len = length
int = integer
str = string (by convention, we put a string between quotes – single or double)

type = will return one of these data types

>>> type("hello")
<class 'str'>

the dot ” . ” connects the string with

>>> "I'm in graduate school".split()
["I'm", 'in', 'graduate', 'school']
>>> len(["I'm", 'in', 'graduate', 'school'])
4
>>> len("I'm in graduate school".split())
4

in lists, the elements are ordered, Python starts with [0]
The number in brackets is called the index, and points to a specific element within the list

>>> "I'm in graduate school".split()[3]
'school'
>>> 

here’s an exciting oddity that I accidentally discovered:

>>> "I'm in graduate school".split()[2][3]
'd'

the second index returns the third character within the second element split from the string

Ctrl+C will escape you from Python back to cmd (or Terminal, as the case may be)

Leave a Reply

Your email address will not be published. Required fields are marked *