Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
222 views

Python CheatSheet PDF

This document provides a summary of key concepts in Python including general information, numbers, strings, lists, tuples, dictionaries, conditional statements, functions, classes, and loops. It explains how to define and call functions, create and access objects and their attributes, use built-in data types like lists and dictionaries, iterate with for and while loops, and more in 3 sentences or less.

Uploaded by

Tareq Mufaddi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

Python CheatSheet PDF

This document provides a summary of key concepts in Python including general information, numbers, strings, lists, tuples, dictionaries, conditional statements, functions, classes, and loops. It explains how to define and call functions, create and access objects and their attributes, use built-in data types like lists and dictionaries, iterate with for and while loops, and more in 3 sentences or less.

Uploaded by

Tareq Mufaddi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python TunnelsUp.com v1.

1
General Information Numbers
Whitespace matters! Indent where needed. total = 3 * 3 # 9
Import modules with "import modulename" total = 5 + 2 * 3 # 11
# This is a comment cost = 1.50 + 3.75 # 5.25
print("Hello, World!") # prints to screen total = int("9") + 1 # 10
Conditional Statements Strings
if isSunny: title = 'Us and them'
print('It's sunny!') # most list operations work on strings
elif 90 <= temp < 100 and bath > 80: title[0] # 'U'
print('Bath is hot and full!') len(title) # 11
elif not ((job == 'qa') or (usr == 'adm')): title.split(' ') # ['Us', 'and', 'them']
print('Match if not qa or adm') ':'.join(['A','B','C']) # 'A:B:C'
else: nine = str(9) # convert int to string
print('No match. Job is ' + job) title.replace('them', 'us') # Us and us
Lists Tuples
scores = ['A', 'C', 90, 75, 'C'] Like lists, except they cannot be changed
scores[0] # 'A' tuple1 = (1,2,3,"a","z") # Creates tuple
scores[1:3] # 'C', 90 tuple1[3] # 'a'
scores[2:] # 90, 75, 'C' Dictionaries
scores[:1] # 'A'
scores[:-1] # 'A', 'C', 90, 75 votes = {'red': 3, 'blue': 5}
len(scores) # 5 votes.keys() # ['blue', 'red']
scores.count('C') # 2 votes['gold'] = 4 # add a key/val
scores.sort() # 75, 90, 'A', 'C', 'C' del votes['gold'] # deletes key
scores.remove('A') # removes 'A' votes['blue'] = 6 # change value
scores.append(100) # Adds 100 to list len(votes) # 2
scores.pop() # removes the last item votes.values() # [6, 3]
scores.pop(2) # removes the third item 'green' in votes # False
75 in scores # True votes.has_key('red') # True

For Loops While Loops
grades = ['A', 'C', 'B', 'F'] i = 0
for grade in grades: # iterate over all vals while True:
print (grade) i += 1
if i == 3:
for k,v in enumerate(grades): # using key value pair continue # go to next loop
if v=='F': if i == 7:
grades[k]='A' # change all Fs to As break # end loop
print(i) # 1 2 4 5 6
inv = {'apples': 7, 'peaches': 4} Class
for fruit, count in inv.items(): # using dictionaries
class Person:
print("We have {} {}".format(count, fruit))
def __init__(self, name, age):
self.name = name
for i in range(10): # 0 to 9 counting by 1s
self.age = age
for i in range(5, 10): # 5 to 9 counting by 1s
for i in range(9, 2, -1): # 9 to 3 decreasing by 1s
def birthYear(self):
Functions return year - self.age
def sumNums(numOne, numTwo = 0):
return numOne + numTwo user = Person('Jimmi', 27)
user.name = 'Jim'
print(sumNums(3,4)) # 7 print(user.name) # prints Jim
print(sumNums(3)) # 3 print(user.birthYear())

You might also like