Basics Flashcards
Remember Python controls
What does a variable do?
Stores a piece of data and gives it a name, to be recalled in a script.
What is a boolean?
Data type that can only be True or False.
#
One-lined comment
”””
”"”Multi-line comment”””
addition subtraction multiplication division exponent modulo (remainder from division)
\+ - * / ** %
str()
Data type that can contain letters, numbers, and symbols. Strings are in “quotation marks”.
Correct the string: ‘There’s a man in my pants!’
‘There's a man in my pants!’
[index]
The assigned number for characters in a string. First number is 0.
len()
Gets the length (number of characters) in a string.
lower()
Makes entire string lowercase.
upper()
Makes entire string uppercase.
str()
Turns variable into a string.
len(lion) and str(lion) but…
lion.upper() and lion.lower()
Example of concatenation
print “Heaven” + “and” + “Hell”
print “%s doesn’t know that %s is out to get %s!” % (“Bob”, “Evan”, “Chris”)
Bob doesn’t know that Evan is out to get Chris!
Retrieve date and time
from datetime import datetime
print datetime.now()
Print date and time in mm/dd/yyyy format
rom datetime import datetime
now = datetime.now()
print ‘%s/%s/%s’ % (now.month, now.day, now.year)
==
!=
<
> =
equal to not equal to less than less than or equal to greater than greater than or equal to
‘and’ boolean operator
True and True is True
True and False is False
False and True is False
False and False is False
‘or’ boolean operator
True or True is True
True or False is True
False or True is True
False or False is False
‘not’ boolean operator
Not True is False
Not False is True
Order of boolean operators
- not
- and
- or
Correct this: def greater_less_equal_5(answer) if (answer > 5) return 1 elif (answer
def greater_less_equal_5(answer): if (answer > 5): return 1 elif (answer
slice into “and”:
s = “Sandra”
s = “Sandra”
print s[1:3]