Python 3 Essentials: A Beginner's Cheat Sheet, Cheat Sheet of Programming Languages

Phython 3 quick cheat sheet for beginners

Typology: Cheat Sheet

2019/2020

Uploaded on 10/09/2020

stefan18
stefan18 🇺🇸

4.2

(36)

278 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python 3 Beginner's Reference Cheat Sheet
Special
characters
# coment
\n new line
\<char> scape char
Numeric
operators
+ addition
- subtraction
* multiplication
/ division
** exponent
% modulus
// floor division
Boolean
operators
== equal
!= different
> higher
< lower
>= higher or equal
<= lower or equal String methods
string.upper() converts to uppercase
string.lower() converts to lowercase
string.count(x) counts how many
times x appears
string.find(x) position of the x first
occurrence
string.replace(x,y)
replaces x for y
string.strip(x) returns a list of values
delimited by x
string.join(L) returns a string with L
values joined by string
string.format(x) returns a string that
includes formatted x
List methods
list.append(x) adds x to the end of the list
list.extend(L) appends L to the end of the list
list.insert(i,x) inserts x at i position
list.remove(x) removes the first list item whose
value is x
list.pop(i) removes the item at position i and
returns its value
list.clear() removes all items from the list
list.index(x) returns a list of values delimited
by x
list.count(x) returns a string with list values
joined by S
list.sort() sorts list items
list.reverse() reverses list elements
list.copy() returns a copy of the list
Dictionary methods
dict.keys() returns a list of keys
dict.values() returns a list of values
dict.items() returns a list of pairs (key,value)
dict.get(k) returns the value associtated to
the key k
dict.pop() removes the item associated to
the key and returns its value
dict.update(D) adds keys-values (D) to dictionary
dict.clear() removes all keys-values from the
dictionary
dict.copy() returns a copy of the dictionary
Main data types
boolean = True / False
integer = 10
float = 10.01
string = “123abc”
list = [ value1, value2, … ]
dictionary = { key1:value1, key2:value2, …}
String operations
string[i] retrieves character at position i
string[-1] retrieves last character
string[i:j] retrieves characters in range i to j
dict = {} defines an empty dictionary
dict[k] = x stores x associated to key k
dict[k] retrieves the item with key k
del dict[k] removes the item with key k
List operations
list = [] defines an empty list
list[i] = x stores x with index i
list[i] retrieves the item with index I
list[-1] retrieves last item
list[i:j] retrieves items in the range i to j
del list[i] removes the item with index i
Dictionary operations
and logical AND
or logical OR
not logical NOT
Comparison
operators
Alvaro Sebastian
http://www.sixthresearcher.com
Legend: x,y stand for any kind of data values, s for a string, n for a number, L for a list where i,j are list indexes, D stands for a dictionary and k is a dictionary key.
pf2

Partial preview of the text

Download Python 3 Essentials: A Beginner's Cheat Sheet and more Cheat Sheet Programming Languages in PDF only on Docsity!

Python 3 Beginner's Reference Cheat Sheet

Special

characters

coment

\n new line \ scape char

Numeric

operators

  • addition
  • subtraction
  • multiplication / division ** exponent % modulus // floor division

Boolean

operators

== equal != different > higher < lower >= higher or equal

<= lower or equal String methods

string.upper() converts to uppercase string.lower() converts to lowercase string.count(x) counts how many times x appears string.find(x) position of the x first occurrence string.replace(x,y) replaces x for y string.strip(x) returns a list of values delimited by x string.join(L) returns a string with L values joined by string string.format(x) returns a string that includes formatted x

List methods

list.append(x) adds x to the end of the list list.extend(L) appends L to the end of the list list.insert(i,x) inserts x at i position list.remove(x) removes the first list item whose value is x list.pop(i) removes the item at position i and returns its value list.clear() removes all items from the list list.index(x) returns a list of values delimited by x list.count(x) returns a string with list values joined by S list.sort() sorts list items list.reverse() reverses list elements list.copy() returns a copy of the list

Dictionary methods

dict.keys() returns a list of keys dict.values() returns a list of values dict.items() returns a list of pairs (key,value) dict.get(k) returns the value associtated to the key k dict.pop() removes the item associated to the key and returns its value dict.update(D) adds keys-values (D) to dictionary dict.clear() removes all keys-values from the dictionary dict.copy() returns a copy of the dictionary

Main data types

boolean = True / False

integer = 10

float = 10.

string = “123abc”

list = [ value1, value2, … ]

dictionary = { key1:value1, key2:value2, …}

String operations

string[i] retrieves character at position i

string[-1] retrieves last character

string[i:j] retrieves characters in range i to j

dict = {} defines an empty dictionary dict[k] = x stores x associated to key k dict[k] retrieves the item with key k del dict[k] removes the item with key k

List operations

list = [] defines an empty list list[i] = x stores x with index i list[i] retrieves the item with index I list[-1] retrieves last item list[i:j] retrieves items in the range i to j del list[i] removes the item with index i

Dictionary operations

and logical AND

or logical OR

not logical NOT

Comparison

operators

Alvaro Sebastian http://www.sixthresearcher.com

Legend: x,y stand for any kind of data values, s for a string, n for a number, L for a list where i,j are list indexes, D stands for a dictionary and k is a dictionary key.

if :

else if :

else:

if in :

try:

except :

else:

Data validation

while :

def function():

return

for in :

for in

range(start,stop,step):

Loop control

statements

break finishes loop

execution

continue jumps to next

iteration

pass does nothing

Built-in functions

print(x, sep='y') prints x objects separated by y

input(s) prints s and waits for an input that will be returned

len(x) returns the length of x (s, L or D)

min(L) returns the minimum value in L

max(L) returns the maximum value in L

sum(L) returns the sum of the values in L

range(n1,n2,n) returns a sequence of numbers from n1 to n2 in steps of n

abs(n) returns the absolute value of n

round(n1,n) returns the n1 number rounded to n digits

type(x) returns the type of x (string, float, list, dict …)

str(x) converts x to string

list(x) converts x to a list

int(x) converts x to a integer number

float(x) converts x to a float number

help(s) prints help about x

map(function, L) Applies function to values in L

Python 3 Beginner's Reference Cheat Sheet

Alvaro Sebastian http://www.sixthresearcher.com

Legend: x,y stand for any kind of data values, s for a string, n for a number, L for a list where i,j are list indexes, D stands for a dictionary and k is a dictionary key.

Reading and

writing files

f = open(,'w')

f.write()

f.close()

f = open( ,‘r')

f.read()

f.readline()

f.close()

f = open( ,’r’) for line in f:

f.close() import os os.getcwd() os.makedirs() os.chdir() os.listdir()

Running external

programs

Working with files

and folders

for key, value in

dict.items():

Conditional

statements

Loops Functions

import module

module.function()

Modules

from module import *

function()

import os

os.system()