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 for Beginners – Cheat Sheet, Cheat Sheet of Advanced Computer Programming

Learn python basic through this cheat sheet including data types, numerical operators, operations, list and string methods

Typology: Cheat Sheet

2020/2021
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 04/23/2021

millyx
millyx 🇺🇸

4.7

(9)

249 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python for Beginners Cheat Sheet
Data types and Collections
i n t e g e r 10
f l o a t 3. 1 4
b o o l e a n Tru e / F a l s e
s t r i n g a b c d e
l i s t [ 1, 2, 3 , 4 , 5 ]
t u p l e ( 1, 2, a’ , b )
s e t { ‘a , b ’, ’c }
d i c t i o n a ry { ‘a :1 , ‘b :2 }
Numerical Operators
+ad d i t i o n
-s ub t ra c ti o n
*mu l t i p l i c at i o n
/di v i s i o n
** exp o n e n t
%mod u l u s
// f lo o r d i vi s io n
Comparison Operators
<les s
<= l es s o r eq u al
>gre a t e r
>= g re a te r or eq u a l
== eq u a l
!= n ot eq u a l
Logical Operators
a n d l og i ca l AN D
or l og i ca l OR
no t l o gi c al N OT
Membership Operators
in v al u e i n o b je c t
n ot i n v al u e n o t i n o b je c t
Operations
S t r i n g s :
s[i ] i :t h it e m o f s
s [ - 1] l as t i t e m o f s
L i s t s :
l = [ ] d ef i ne e mp t y l i st
l [ i : j ] s li c e i n r a ng e i t o j
l [i ] = x re p l ac e i w i t h x
l [ i : j :k ] s li c e r a ng e i t o j, st e p k
D i c t i o n a ri e s :
d = { } c re a te e mp t y d i c ti o na r y
d[i ] r et r ie v e i t em w it h ke y i
d [ i ] = x s to r e x to k ey I
i i n d i s k ey i i n d i ct i on a ry
Index starts at 0
String Methods
s . s t r i p() r e m ov e tr a il i ng w hi t es p ac e
s . s p l i t ( x) r et u rn l is t , d el i m it e r x
s . j o i n (l) r et u rn s tr i ng , d e l im i te r s
s . s t a r t sw i t h ( x ) r et u rn T ru e i f s s ta r ts wi t h x
s . e n d s w i th ( x ) r et u rn T ru e i f s e nd s wi t h x
s . u p p e r () r et u rn c op y , u p pe r ca s e o n l y
s . l o w e r () re t u rn co p y , l ow e r ca s e o n ly
List Methods
l . a p p e n d ( x) a pp e nd x t o en d o f li s t
l .i n s er t ( I, x ) in s er t x a t p o si t io n i
l . r e m o v e (x ) r em o ve f ir s t o c cu r re n ce o f x
l . r e v er s e () r e ve r se l is t i n pl a ce
Dictionary Methods
d . k e y s () r et u rn s a l is t o f ke y s
d . v a l u e s () re t u rn s a l is t o f va l ue s
d . i t em s () r e tu r ns a l i s t o f ( ke y , v a lu e )
Conditional Statements
if co n d it i o n:
<co d e >
e l i f co n d it i o n :
<co d e >
e l s e :
<co d e >
Import from Module
f ro m mo d u le im p o rt f un c im p or t fu n c
f ro m mo d u le im p o rt f un c a s f i mp o rt f un c a s f
pf2
Discount

On special offer

Partial preview of the text

Download Python for Beginners – Cheat Sheet and more Cheat Sheet Advanced Computer Programming in PDF only on Docsity!

Python for Beginners – Cheat Sheet

Data types and Collections i n t e g e r 1 0 f l o a t 3. 1 4 b o o l e a n T r u e / F a l s e s t r i n g ‘ a b c d e ’ l i s t [ 1 , 2 , 3 , 4 , 5 ] t u p l e ( 1 , 2 , ‘ a ’ , ‘ b ’ ) s e t { ‘ a ’ , ’ b ’ , ’ c ’ } d i c t i o n a r y { ‘ a ’ : 1 , ‘ b ’ : 2 } Numerical Operators

  • a d d i t i o n
  • s u b t r a c t i o n
  • m u l t i p l i c a t i o n / d i v i s i o n
    • e x p o n e n t % m o d u l u s / / f l o o r d i v i s i o n Comparison Operators < l e s s < = l e s s o r e q u a l

      g r e a t e r = g r e a t e r o r e q u a l = = e q u a l ! = n o t e q u a l Logical Operators a n d l o g i c a l A N D o r l o g i c a l O R n o t l o g i c a l N O T Membership Operators i n v a l u e i n o b j e c t n o t i n v a l u e n o t i n o b j e c t Operations S t r i n g s : s [ i ] i : t h i t e m o f s s [ - 1 ] l a s t i t e m o f s L i s t s : l = [ ] d e f i n e e m p t y l i s t l [ i : j ] s l i c e i n r a n g e i t o j l [ i ] = x r e p l a c e i w i t h x l [ i : j : k ] s l i c e r a n g e i t o j , s t e p k D i c t i o n a r i e s : d = { } c r e a t e e m p t y d i c t i o n a r y d [ i ] r e t r i e v e i t e m w i t h k e y i d [ i ] = x s t o r e x t o k e y I i i n d i s k e y i i n d i c t i o n a r y Index starts at 0 String Methods s. s t r i p ( ) r e m o v e t r a i l i n g w h i t e s p a c e s. s p l i t ( x ) r e t u r n l i s t , d e l i m i t e r x s. j o i n ( l ) r e t u r n s t r i n g , d e l i m i t e r s s. s t a r t s w i t h ( x ) r e t u r n T r u e i f s s t a r t s w i t h x s. e n d s w i t h ( x ) r e t u r n T r u e i f s e n d s w i t h x s. u p p e r ( ) r e t u r n c o p y , u p p e r c a s e o n l y s. l o w e r ( ) r e t u r n c o p y , l o w e r c a s e o n l y List Methods l. a p p e n d ( x ) a p p e n d x t o e n d o f l i s t l. i n s e r t ( I , x ) i n s e r t x a t p o s i t i o n i l. r e m o v e ( x ) r e m o v e f i r s t o c c u r r e n c e o f x l. r e v e r s e ( ) r e v e r s e l i s t i n p l a c e Dictionary Methods d. k e y s ( ) r e t u r n s a l i s t o f k e y s d. v a l u e s ( ) r e t u r n s a l i s t o f v a l u e s d. i t e m s ( ) r e t u r n s a l i s t o f ( k e y , v a l u e ) Conditional Statements i f c o n d i t i o n : < c o d e > e l i f c o n d i t i o n : < c o d e > e l s e : < c o d e > Import from Module f r o m m o d u l e i m p o r t f u n c i m p o r t f u n c f r o m m o d u l e i m p o r t f u n c a s f i m p o r t f u n c a s f

Python for Beginners – Cheat Sheet

Built-in Functions f l o a t ( x ) c o n v e r t x t o f l o a t i n t ( x ) c o n v e r t x t o i n t e g e r s t r ( x ) c o n v e r t x t o s t r i n g s e t ( x ) c o n v e r t x t o s e t t y p e ( x ) r e t u r n s t y p e o f x l e n ( x ) r e t u r n s l e n g t h o f x m a x ( x ) r e t u r n s m a x i m u m o f x m i n ( x ) r e t u r n s m i n i m u m o f x s u m ( x ) r e t u r n s s u m o f v a l u e s i n x s o r t e d ( x ) r e t u r n s s o r t e d l i s t r o u n d ( x , d ) r e t u r n s x r o u n d e d t o d p r i n t ( x ) p r i n t o b j e c t x Loops w h i l e c o n d i t i o n : < c o d e > f o r v a r i n l i s t : < c o d e > C o n t r o l s t a t e m e n t s : b r e a k t e r m i n a t e l o o p c o n t i n u e j u m p t o n e x t i t e r a t i o n p a s s d o e s n o t h i n g Functions d e f N a m e ( p a r a m 1 , p a r a m 2 = v a l ) : < c o d e >

p a r a m 2 o p t i o n a l , d e f a u l t : v a l

r e t u r n < d a t a > Reading and Writing Files f h = o p e n ( < p a t h > , ’ r ’ ) f o r l i n e i n f h : < c o d e > f h. c l o s e ( ) o u t = o p e n ( < p a t h > , ’ w ’ ) o u t. w r i t e ( < s t r > ) o u t. c l o s e ( ) Regular Expressions i m p o r t r e p = r e. c o m p i l e ( p a t t e r n ) c o m p i l e s e a r c h q u e r y p. s e a r c h ( t e x t ) s e a r c h f o r a l l m a t c h e s p. s u b ( s u b , t e x t ) s u b s t i t u t e m a t c h w i t h s u b

. a n y o n e c h a r a c t e r

  • r e p e a t p r e v i o u s 0 o r m o r e t i m e s
  • r e p e a t p r e v i o u s 1 o r m o r e t i m e s ? r e p e a t p r e v i o u s 0 o r 1 t i m e s \ d a n y d i g i t \ s a n y w h i t e s p a c e [ a b c ] a n y c h a r a c t e r i n t h i s s e t { a , b , c } [ ^ a b c ] a n y c h a r a c t e r * n o t * i n t h i s s e t [ a - z ] a n y l e t t e r b e t w e e n a a n d z a | b a o r b String Formatting “ P u t { } i n t o a { } ”. f o r m a t ( “ v a l u e s ” , “ s t r i n g ” ) ‘ P u t v a l u e s i n t o a s t r i n g ’ “ P u t w h i t e s p a c e a f t e r : { : < 1 0 } , o r b e f o r e : { : > 1 0 } ”. f o r m a t ( “ a ” , ” b ” ) ‘ P u t w h i t e s p a c e a f t e r : a , o r b e f o r e : b ’ “ P u t w h i t e s p a c e a r o u n d : { : ^ 1 0 }. ”. f o r m a t ( “ c ” ) ‘ P u t w h i t e s p a c e a r o u n d : c. sys.argv i m p o r t s y s i m p o r t m o d u l e s y s. a r g v [ 0 ] n a m e o f s c r i p t s y s. a r g v [ 1 ] f i r s t c m d l i n e a r g