Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python Cheat Sheet, Cheat Sheet of Programming Languages

Main topics of this Python cheat sheet are: primitives, collections, Control Statements, fonctions

Typology: Cheat Sheet

2019/2020

Uploaded on 10/09/2020

damyen
damyen 🇺🇸

4.4

(27)

274 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python is a beautiful language. It's easy to learn and fun, and its syntax is simple
yet elegant. Python is a popular choice for beginners, yet still powerful enough to
to back some of the world’s most popular products and applications from
companies like NASA, Google, Mozilla, Cisco, Microsoft, and Instagram, among
others. Whatever the goal, Python’s design makes the programming experience
feel almost as natural as writing in English.
Check out Real Python to learn more about Python and web development.
Note: This article applies to Python 2.7x specifically.
Email your questions or feedback to info@realpython.com.
Python has integers and floats. Integers are simply whole numbers, like 314, 500,
and 716. Floats, meanwhile, are fractional numbers like 3.14, 2.867, 76.88887.
You can use the type method to check the value of an object.
>>> type(3)
<type 'int'>
>>> type(3.14)
<type 'float'>
>>> pi = 3.14
>>> type(pi)
<type 'float'>
Python Cheat Sheet
1. Primitives
Numbers
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python Cheat Sheet and more Cheat Sheet Programming Languages in PDF only on Docsity!

Python is a beautiful language. It's easy to learn and fun, and its syntax is simple

yet elegant. Python is a popular choice for beginners, yet still powerful enough to

to back some of the world’s most popular products and applications from

companies like NASA, Google, Mozilla, Cisco, Microsoft, and Instagram, among

others. Whatever the goal, Python’s design makes the programming experience

feel almost as natural as writing in English.

Check out Real Python to learn more about Python and web development.

Note: This article applies to Python 2.7x specifically.

Email your questions or feedback to info@realpython.com.

Python has integers and floats. Integers are simply whole numbers, like 314, 500,

and 716. Floats, meanwhile, are fractional numbers like 3.14, 2.867, 76.88887.

You can use the type method to check the value of an object.

type(3) <type 'int'> type(3.14) <type 'float'> pi = 3. type(pi) <type 'float'>

Python Cheat Sheet

1. Primitives

Numbers

In the above example, pi is the variable name, while 3.14 is the value.

You can use the basic mathematical operators:

num = 3 num = num - 1 print num 2 num = num + 10 print num 12 num += 10 print num 22 num -= 12 print num 10 num *= 10 num 100

What happens when you divide 9 by 4?

What is the actual answer? 2 remainder 1 right? Or: 2.

In Python 2.7x, when you divide a whole number by a whole number and the

A character is anything you can type on the keyboard in one keystroke, like a

letter, a number, or a backslash.

Python recognizes single and double quotes as the same thing, the beginning and

ends of the strings.

“string list” ‘string list’ ‘string list’ ‘string list’

Now what if you have a quote in the middle of the string? Python needs help to

recognize quotes as part of the English language and not as part of the Python

language.

“I can’t do that” “I can’t do that” “He said \“no\” to me” “He said “no” to me”

Now you can also join strings with use of variables as well.

a = “first” b = “last” a + b ‘firstlast’

If you want a space in between, you can change a to the word with a space after.

a = “first ” a + b

“first last”

There are also different string methods for you to choose from as well - like

upper , lower , replace , and count.

Upper does just what it sounds like, changes your string to have uppercase

letters.

w='woah!' w.upper() 'WOAH!'

Lower is just that as well, keeping your string in lower case letters.

w='WOAH!' w.lower() 'woah!'

Replace allows you to replace any character with another character.

r='rule' r.replace('r','m') 'mule'

Count lets you know how many times x appears in the string (can be numbers or

a string of words as well).

numbers=['1','2','1','2','2'] numbers.count('2')

3

You can also format strings with the format method.

"{0} is a lot of {1}".format("Python", "fun!") “string list” ‘string list’ >>> ‘string list’ ‘string list’ ## Now what if you have a quote in the middle of the string? Python needs help to ## recognize quotes as part of the English language and not as part of the Python ## language. >>> “I can’t do that” “I can’t do that” >>> “He said \“no\” to me” “He said “no” to me” ## Now you can also join strings with use of variables as well. >>> a = “first” >>> b = “last” >>> a + b ‘firstlast’ ## If you want a space in between, you can change a to the word with a space after. >>> a = “first ” >>> a + b “first last” ## There are also different string methods for you to choose from as well - like ## upper , lower , replace , and count. ## Upper does just what it sounds like, changes your string to have uppercase ## letters. >>> w='woah!' >>> w.upper() 'WOAH!' ## Lower is just that as well, keeping your string in lower case letters. >>> w='WOAH!' >>> w.lower() 'woah!' ## Replace allows you to replace any character with another character. >>>r='rule' >>>r.replace('r','m') 'mule' ## Count lets you know how many times x appears in the string (can be numbers or ## a string of words as well). >>>numbers=['1','2','1','2','2'] >>>numbers.count('2') 3 ## You can also format strings with the format method. >>> "{0} is a lot of {1}".format("Python", "fun!") 'Python is a lot of fun!'

Boolean values are simply True or False.

Check to see if a value is equal to another value with two equal signs.

Booleans

Lists are containers for holding values.

fruits = ['apple','lemon','orange','grape'] fruits ['apple', 'lemon', 'orange', 'grape']

To access the elements in the list you can use the placement in the list as an

idicator. This means numbering the items aligned with their placement in the list.

But, you must remember that the list starts with 0.

fruits[2] ‘orange’

If the list is longer and you need to count from the end you can also do that.

fruits[-2] ‘orange’

Now, sometimes lists can get long and you want to keep track of how many

elements you have in your list. To find this, use len which is the length function.

len(fruits) 4

Use append to add a new object to the end of the list and pop to remove objects

from the end.

2. Collections Lists

fruits.append('blueberry') fruits ['apple', 'lemon', 'orange', 'grape', 'blueberry'] fruits.append('tomato') fruits ['apple', 'lemon', 'orange', 'grape', 'blueberry', 'tomato'] fruits.pop() 'tomato' fruits ['apple', 'lemon', 'orange', 'grape', 'blueberry']

Check to see if a value exists using in.

'apple' in fruits True 'tomato' in fruits False

A dictionary optimizes element lookups. It uses keys and values, instead of

numbers as placeholders. Each key must have a value. You can used a word to

look up a value.

words={'apple':'red','lemon':'yellow'} words {'lemon': 'yellow', 'apple': 'red'} words['apple'] 'red' words['lemon'] 'yellow'

This will also work with numbers.

Dictionaries

num = 20 if num == 20: ... print 'the number is 20' ... else:

... print 'the number is not 20' ... the number is 20

num = 21 if num == 20: ... print 'the number is 20' ... else: ... print 'the number is not 20' ... the number is not 20

You can also add an elif clause to add another condition.

num = 21 if num == 20: ... print 'the number is 20' ... elif num > 20: ... print 'the number is greater then 20' ... else: ... print 'the number is less then 20' ... the number is greater then 20

There are 2 kinds of loops used in Python. The For loop and the While loop. For

loops are traditionally used when you have a piece of code which you want to

repeat n number of times. They are also commonly used to loop or iterate over

lists.

Loops

colors =('red','blue','green') colors ('red', 'blue', 'green') for favorite in colors: ... print "I love " + favorite ...

'tomato' in fruits False ## A dictionary optimizes element lookups. It uses keys and values, instead of ## numbers as placeholders. Each key must have a value. You can used a word to ## look up a value. >>> words={'apple':'red','lemon':'yellow'} >>> words {'lemon': 'yellow', 'apple': 'red'} >>> words['apple'] 'red' >>> words['lemon'] 'yellow' ## This will also work with numbers. Dictionaries >>> num = 20 >>> if num == 20: ... print 'the number is 20' ... else: ... print 'the number is not 20' ... the number is 20 >>> num = 21 >>> if num == 20: ... print 'the number is 20' ... else: ... print 'the number is not 20' ... the number is not 20 ## You can also add an elif clause to add another condition. >>> num = 21 >>> if num == 20: ... print 'the number is 20' ... elif num > 20: ... print 'the number is greater then 20' ... else: ... print 'the number is less then 20' ... the number is greater then 20 ## There are 2 kinds of loops used in Python. The For loop and the While loop. For ## loops are traditionally used when you have a piece of code which you want to ## repeat n number of times. They are also commonly used to loop or iterate over ## lists. Loops >>> colors =('red','blue','green') >>> colors ('red', 'blue', 'green') >>> for favorite in colors: ... print "I love " + favorite ... I love red I love blue I love green

While loops, like the For Loop, are used for repeating sections of code - but unlike

a for loop, the while loop will not run n times, but until a defined condition is met.

num = 1 num 1 while num <=5: ... print num ... num += 1 ... 1 2 3 4 5

Functions are blocks of reusable code that perfrom a single task.

You use def to define (or create) a new function then you call a function by

adding parameters to the function name.

4. Functions