
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
msg = 'I grok Python!' for i in range(len(msg)): print(i, msg[i]). Repeat a block over list (or string) indices for i in range(10): print(i).
Typology: Lecture notes
1 / 1
This page cannot be seen from the preview
Don't miss anything!

celsius = int(input('Temp. in Celsius: ')) Ask the user for a temperature in degrees Celsius fahrenheit = celsius*9/5 + 32 Calculate the conversion print(fahrenheit, 'Fahrenheit') Output the result
'e' in msg Is a character in a string? 'ell' in msg Is a string in another string? msg.upper() Convert to uppercase msg.count('l') Count a character in a string msg.replace('l','X') Replace a character or string msg.replace('l','') Delete a character or string if msg < 'n': print('a-m') else: print('n-z') Less than another string? strings are compared character at a time ( lexicographic order ) msg = 'hello' if msg == 'hello': print('howdy') Compare two strings
also lower and title msg.islower() Is the string all lowercase? also isupper and istitle range(10) Count from 0 to 9 range(1, 11) Count from 1 to 10 range(10, 0, -1) Count from 10 down to 1 range(0, 11, 2) Count 2 at a time to 10 range(10, 0, -2) Count down 2 at a time for c in 'Hello': print(c) Repeat a block over a string for c in 'Hello': print(c, end=' ') print('!') Keep printing on one line msg = 'I grok Python!' for i in range(len(msg)): print(i, msg[i]) Repeat a block over list (or string) indices for i in range(10): print(i) Repeat a block 10 times total = 0 for i in range(10): total = total + i print(total) Sum the numbers 0 to 9
range starts from 0 and goes up to, but not including, 10 x == 3 Are two values equal? True False The answer is a Boolean : or x != 3 Are two values not equal? x < 3 Less than another? x > 3 Greater than another? x <= 3 Less than or equal to? x >= 3 Greater than or equal to? mark = 80 if mark >= 65: print('credit') elif mark >= 50: print('pass') else: print('fail') Decide between many blocks ā£elif can be used without else ā£elif can be used many times mark = 80 if mark >= 50: print('pass') else: print('fail') Decide between two blocks x = 3 if x == 3: print('x is 3') Decide to run a block (or not)
two equals signs, not one
Addition and subtraction 259/5 + 32 Multiplication and division 2* Powers ( 2 to the power of 8 ) str(365) Convert integer to string
celsius = 25 Creating a variable celsius*9/5 + 32 Using a variable
'perfect' Single quoted "credit" Double quoted 'Hello' + 'World' Add ( concatenate ) strings 'Echo...'* Multiply string by integer len('Hello') Length of a string int('365') Convert string to integer '''Hello, World!''' Multi-line
print('Hello, world!') Print a message name = input('What is your name? ') Asking the user for a string num = int(input('Enter a number: ')) Asking the user for a whole number (an integer) ndays = 365 print('There are', ndays, 'in a year') Print multiple values (of different types)
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) return [f(self.for else: return funcs(sel n_(self):