wikibooks Python 1 Flashcards
print “hello world”
print (‘hello world’)
Variable definition
something that holds a value that can change
Variable in which red is equal to 5
red = 5
Variable in which blue equals 10
blue = 10
Program which runs with the variables red and blue based on previous variable assignment placing red and then blue
print (red, blue)
5 10
Name of Variable location
left of equals sign
Value of Variable location
right of equals sign
String definition
a list of characters in order
Three ways to declare a string
Single quotes (‘) Double quotes (“) Triple quotes (“””)
Escaping the symbol definition
Placing a backslash before a symbol to show that the symbol should be included in the string
Escaping a quotation mark example
print»_space;> (“So I said, \”You don’t know me! You’ll never understand me!\””)
Concatenation operator
+
Concatenation example
> > > print (“Hello, “ + “world!”)
Hello, world!
Repeat strings operator
*
Repeat strings example
> > > print (“bouncy, “ * 3)
bouncy, bouncy, bouncy,
Function which finds the length of a string
len()
Finding string length example
> > > print (len(“Hello, world!”))
13
Function that turns a string into an integer
int()
Function that turns an integer into a string
str()
Function which outputs the information to the user
print()
Function which asks the user for a response and returns that response
input()
Are variables case-sensitive?
yes
Can spaces be substituted for tabs?
no
Object definition
aggregations of code and data which typically represent the pieces in a conceptual model of a system
Attributes definition
represent various pieces of code and data which make up the object
How to access attribute
object.attribute
Example of upper attribute
‘bob’.upper
Program to make ‘bob’ into ‘BOB’
‘bob’.upper()
Scope definition
a region of code in which a name can be used and outside of which the name cannot be easily accessed
2 ways of delimiting scope
Functions
Modules
Namespace definition
A particular place in which names specific to a module reside
Seven Common Sequence Types
strings, Unicode strings, lists, tuples, bytearrays, buffers, xrange objects
2 Containers for sequential data
Dictionaries
Sets
Accessing characters in strings and elements in arrays
[ ]
Accessing characters in strings example
> > > “Hello, world!” [0]
‘H’
“Hello, world!” [4]
‘o’
Accessing characters in strings negative example
> > > “Hello, world!”[-2]
‘d’
“Hello, world!”[-13]
‘H’
Slicing definition
Using indexes to divide a string into a smaller substring
Slicing Operator
[ : ]
Slicing example
> > > “Hello, world!” [3:9]
‘lo, wo’
List definition
a list of values, organized in order
List operator
[ ]
Values in list separated by
,
List example
spam = [“bacon”, “eggs”, 42]
How to access bacon from the list: spam = [“bacon”, “eggs”, 42]
> > > spam[0]
The length of spam. spam = [“bacon”, “eggs”, 42]
> > > len(spam)
3
Can lists be sliced?
yes
Add items to a list function
append()
Appending items to a list example
> > > spam.append(10)
spam
[‘bacon’, ‘eggs’, 42, 10]
Inserting an item into a list function
insert()
Inserting item into list example
> > > spam.insert(1, ‘and’)
spam
[‘bacon’, ‘and’, ‘eggs’, 42, 10]
Deleting items from a list function
del
Delete item from list example
> > > del spam[1]
spam
[‘bacon’, eggs’, 42, 10]
Tuple definition
list that can’t be changed
Tuple function
,
Tuple example
unchanging = “rocks”, 0, “the universe”
Dictionary definition
Like a list but not bound to numbers
2 parts of a dictionary element
Key
Value
Dictionary operator
{ }
Separator between key and value
:
Dictionary example
> > > definitions = {“guava”: “a tropical fruit”, “python”: “a programming language”}
Pulling up a definition example
> > > definitions[“guava”]
‘a tropical fruit’
Set definition
Like a list but unordered and do not allow duplicate values
Set operator
set([ ])
Set example
> > > mind = set([42, ‘a string’, (23, 4)])
Frozenset definition
an immutable version of a set
Frozenset operator
frozenset([ ])
Frozenset example
> > > frozen=frozenset([‘life’, ‘universe’, ‘everything’])
4 types of data
Numeric
Sequences
Sets
Mappings
4 numeric types
int long (only python 2) float complex
5 sequence types
str bytes (only python 3) byte array (only python 3) list tuple
2 set types
set
frozenset
1 mapping type
dict
Mutable objects definition
objects that can be changed after they’re created
Immutable object definition
objects that can’t be changed after they’re made
8 immutable types
int, float, complex, str, bytes, tuple, frozenset, bool
5 mutable types
array byte array list set dict