Sei sulla pagina 1di 1

Python(3(cheatsheet((the(basics) GROK

LEARNING
Interact$with$the$user$(input$and$output) Text$(strings) Variables
Print$a$message Single$quoted CreaCng$a$variable
print('Hello, world!') 'perfect' celsius = 25
Print$mulCple$values$(of$different$types) Double$quoted Using$a$variable
ndays = 365 "credit" celsius*9/5 + 32
print('There are', ndays, 'in a year')
MulCVline
Asking$the$user$for$a$string
'''Hello,
name = input('What is your name? ') World!''' Whole$numbers$(integers)
Asking$the$user$for$a$whole$number$(an$integer) Add$(concatenate)$strings AddiCon$and$subtracCon
num = int(input('Enter a number: ')) 'Hello' + 'World' 365 + 1 - 2
MulCply$string$by$integer MulCplicaCon$and$division
Decide$between$opCons 'Echo...'*4 25*9/5 + 32
Decide$to$run$a$block$(or$not) Are$two$values$equal? Length$of$a$string Powers$(2(to(the(power(of(8)
x = 3 x == 3 len('Hello') 2**8
if x == 3:
print('x is 3') +two+equals+signs,+not+one Convert$string$to$integer Convert$integer$to$string

Decide$between$two$blocks Are$two$values$not$equal? int('365') str(365)


mark = 80 x != 3
if mark >= 50: Repeat$a$block$(a$fixed$number$of$Cmes)
Less$than$another?
print('pass')
else: x < 3 Repeat$a$block$10$Cmes Count$from$0$to$9
print('fail')
Greater$than$another? for i in range(10): range(10)
Decide$between$many$blocks print(i)
x > 3 range+starts+from+0+and+goes
mark = 80 +++++++up+to,+but,not,including,+10
Sum$the$numbers$0$to$9
if mark >= 65: Less$than$or$equal$to?
print('credit') total = 0
elif mark >= 50: x <= 3 for i in range(10): Count$from$1$to$10
print('pass') total = total + i
Greater$than$or$equal$to? range(1, 11)
else: print(total)
print('fail') x >= 3 Count$from$10$down$to$1
Repeat$a$block$over$a$string
‣elif$can$be$used$without$else The$answer$is$a$Boolean: range(10, 0, -1)
for c in 'Hello':
‣elif$can$be$used$many$Cmes True or False print(c) Count$2$at$a$Cme$to$10
Keep$prinCng$on$one$line range(0, 11, 2)
String$manipulaCon for c in 'Hello': Count$down$2$at$a$Cme
print(c, end=' ')
Compare$two$strings Convert$to$uppercase print('!') range(10, 0, -2)
msg = 'hello' msg.upper() Repeat$a$block$over$list$(or$string)$indices
if msg == 'hello':
print('howdy') also$lower$and$title msg = 'I grok Python!'
for i in range(len(msg)):
Count$a$character$in$$a$string
Less$than$another$string? print(i, msg[i])
msg.count('l')
if msg < 'n':
print('a-m') Replace$a$character$or$string Pu9ng$it$together:$Celsius$to$Fahrenheit$converter
else:
print('n-z') msg.replace('l','X')
Ask$the$user$for$a$temperature$in$degrees$Celsius
strings+are+compared+character Delete$a$character$or$string
+++++++at+a+4me+(lexicographic,order)
celsius = int(input('Temp. in Celsius: '))
msg.replace('l','')
Is$a$character$in$a$string? Calculate$the$conversion
'e' in msg Is$the$string$all$lowercase? fahrenheit = celsius*9/5 + 32

Is$a$string$in$another$string? msg.islower() Output$the$result


'ell' in msg also$isupper$and$istitle print(fahrenheit, 'Fahrenheit')
s = [datetime.
hs.insert(0, lamb

f __init__(self, format
self.format = format

def __getitem__(self, i):


funcs = self._months[i]
if isinstance(i, slice)

Learn&more&in&Intro.&to&Programming(@(groklearning.com
return [f(self.for
else:
return funcs(sel

n__(self):

Potrebbero piacerti anche