Python For Free
Python For Free
Contents
1 Introduction 2
3 Control Flow 2
3.1 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4 Functions 3
5 Lists 4
6 Dictionaries 4
8 File Handling 5
9 Exception Handling 6
13 Conclusion 7
1
1 Introduction
Python is a high-level, interpreted programming language known for its simplicity and readability. It has
a large user community and a wide range of libraries that make it versatile and powerful. This cheatsheet
provides a quick reference for Python programming concepts.
• Boolean: bool
• None: NoneType
3 Control Flow
3.1 Conditional Statements
Conditional statements allow you to perform different actions based on conditions.
1 # If statement
2 if condition :
3 statement ( s )
4
5 # If - else statement
6 if condition :
7 statement ( s )
8 else :
9 statement ( s )
10
2
3.2 Loops
Loops enable repeated execution of a block of code.
1 # For loop
2 for item in sequence :
3 statement ( s )
4
5 # While loop
6 while condition :
7 statement ( s )
8
18 # Continue statement
19 for item in sequence :
20 if condition :
21 continue
22 statement ( s )
23
24 # Range function
25 for i in range ( start , stop , step ) :
26 statement ( s )
27
28 # Nested loops
29 for item in sequence :
30 for element in nested_sequence :
31 statement ( s )
32
39 # Continue statement
40 while condition :
41 if condition :
42 continue
43 statement ( s )
4 Functions
Functions are reusable blocks of code that perform a specific task.
1 # Defining a function
2 def function_name ( parameters ) :
3 statement ( s )
4 return value
5
6 # Function call
7 result = function_name ( arguments )
3
8
9 # Default parameters
10 def function_name ( parameter = value ) :
11 statement ( s )
12
17 # Keyword arguments
18 def function_name (** kwargs ) :
19 statement ( s )
20
21 # Lambda functions
22 lambda arguments : expression
5 Lists
A list is a collection of items that are ordered and mutable.
1 # Creating a list
2 my_list = [ item1 , item2 , item3 ]
3
7 # Slicing a list
8 new_list = my_list [ start : end ]
9
19 # List operations
20 combined_list = list1 + list2
21 repeated_list = my_list * n
6 Dictionaries
A dictionary is an unordered collection of key-value pairs.
1 # Creating a dictionary
2 my_dict = { " key1 " : value1 , " key2 " : value2 }
3
4
12
16 # Dictionary operations
17 keys = my_dict . keys ()
18 values = my_dict . values ()
19 items = my_dict . items ()
16 # Creating a package
17 __init__ . py
18
8 File Handling
Python provides functions for reading from and writing to files.
1 # Opening a file
2 file = open ( filename , mode )
3
8 # Writing to a file
9 file . write ( content )
10
11 # Appending to a file
12 file = open ( filename , " a " )
13 file . write ( content )
14
15 # Closing a file
16 file . close ()
5
9 Exception Handling
Exception handling allows you to handle and manage errors that occur during the execution of your program.
1 # Try - except block
2 try :
3 statement ( s )
4 except ExceptionType :
5 statement ( s )
6
15 # Finally block
16 try :
17 statement ( s )
18 except ExceptionType :
19 statement ( s )
20 finally :
21 statement ( s )
22
23 # Raising an exception
24 raise ExceptionType ( " Error message " )
9 # Creating an object
10 object_name = ClassName ( arguments )
11
12 # Accessing attributes
13 value = object_name . attribute
14
15 # Calling methods
16 object_name . method ( arguments )
6
3 def __init__ ( self , parameters ) :
4 super () . __init__ ( parameters )
5 self . attribute = value
6
10 # Polymorphism
11 class Class1 :
12 def method ( self , parameters ) :
13 statement ( s )
14
15 class Class2 :
16 def method ( self , parameters ) :
17 statement ( s )
18
16 # Finally block
17 try :
18 statement ( s )
19 except ExceptionType :
20 statement ( s )
21 finally :
22 statement ( s )
23
24 # Raising an exception
25 raise ExceptionType ( " Error message " )
13 Conclusion
This Python cheatsheet covers the fundamental concepts and syntax of Python programming. It serves as a
quick reference guide for beginners. Keep exploring and practicing to enhance your Python skills!