

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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Learn python basic through this cheat sheet including data types, numerical operators, operations, list and string methods
Typology: Cheat Sheet
1 / 2
This page cannot be seen from the preview
Don't miss anything!
On special offer
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
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
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 >
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