1
+ # ----------------------------------------------------------------------
2
+ # Reading and Writing Files:
3
+
4
+ # Reading a file
5
+ with open ('file.txt' , 'r' ) as file :
6
+ content = file .read ()
7
+
8
+ # Writing to a file
9
+ with open ('file.txt' , 'w' ) as file :
10
+ file .write ('New content' )
11
+
12
+ # ----------------------------------------------------------------------
13
+ iterable = []
14
+ #for Loop:
15
+ for item in iterable :
16
+ # Code to be executed
17
+ pass
18
+
19
+ # ----------------------------------------------------------------------
20
+ condition = True
21
+ # if Statement:
22
+ if condition :
23
+ # Code to be executed if the condition is true
24
+ pass
25
+
26
+ # ----------------------------------------------------------------------
27
+ # Defining a Function:
28
+ def my_function (parameter1 , parameter2 ):
29
+ # Function code
30
+ return result
31
+
32
+ # ----------------------------------------------------------------------
33
+ # Using Lists:
34
+ my_list = [1 , 2 , 3 , 4 , 5 ]
35
+
36
+ # ----------------------------------------------------------------------
37
+ #Using Dictionaries:
38
+ my_dict = {'key1' : 'value1' , 'key2' : 'value2' }
39
+
40
+ # ----------------------------------------------------------------------
41
+ # while Loop:
42
+ while condition :
43
+ # Code to be executed
44
+ pass
45
+
46
+ # ----------------------------------------------------------------------
47
+ #Using Standard Modules:
48
+ import math
49
+ result = math .sqrt (16 )
50
+
51
+ # ----------------------------------------------------------------------
52
+ # List Comprehension:
53
+ squares = [x ** 2 for x in range (10 )]
54
+
55
+ # ----------------------------------------------------------------------
56
+ # Exception Handling:
57
+ try :
58
+ # Code that may raise an exception
59
+ pass
60
+ except Exception as e :
61
+ # Handling the exception
62
+ pass
63
+
64
+
65
+ # ----------------------------------------------------------------------
66
+ # Working with Strings:
67
+ my_string = "Hello, World!"
68
+
69
+ # ----------------------------------------------------------------------
70
+ # Using Sets:
71
+ my_set = {1 , 2 , 3 }
72
+
73
+ # ----------------------------------------------------------------------
74
+ # Using Tuples:
75
+ my_tuple = (1 , 2 , 3 )
76
+
77
+ # ----------------------------------------------------------------------
78
+ # Getting User Input:
79
+ user_input = input ("Enter something: " )
80
+
81
+ # ----------------------------------------------------------------------
82
+ # Converting Data Types:
83
+ number = int ("42" )
84
+
85
+ # ----------------------------------------------------------------------
86
+ # Finding the Length of a List:
87
+ length = len (my_list )
88
+
89
+ # ----------------------------------------------------------------------
90
+ # Sorting a List:
91
+ sorted_list = sorted (my_list )
92
+
93
+ # ----------------------------------------------------------------------
94
+ # Appending to a List:
95
+ my_list .append (6 )
96
+
97
+ # ----------------------------------------------------------------------
98
+ # Removing an Item from a List:
99
+ my_list .remove (3 )
100
+
101
+ # ----------------------------------------------------------------------
102
+ # Checking Membership in a List:
103
+ if item in my_list :
104
+ # Code to handle membership
105
+ pass
106
+
107
+ # ----------------------------------------------------------------------
108
+ # Creating a Dictionary:
109
+ my_dict = dict (key1 = 'value1' , key2 = 'value2' )
110
+
111
+ # ----------------------------------------------------------------------
112
+ # Accessing Dictionary Values:
113
+ value = my_dict ['key1' ]
114
+
115
+ # ----------------------------------------------------------------------
116
+ # Updating Dictionary Values:
117
+ my_dict ['key1' ] = 'new_value'
118
+
119
+ # ----------------------------------------------------------------------
120
+ # Deleting Dictionary Items:
121
+ del my_dict ['key1' ]
122
+
123
+ # ----------------------------------------------------------------------
124
+ # Checking Dictionary Keys:
125
+ if 'key1' in my_dict :
126
+ # Code to handle key existence
127
+ pass
128
+
129
+ # ----------------------------------------------------------------------
130
+ # Using enumerate with Lists:
131
+ for index , item in enumerate (my_list ):
132
+ # Code using index and item
133
+ pass
134
+
135
+ # ----------------------------------------------------------------------
136
+ # Using zip to Combine Lists:
137
+ list1 ,list2 = []
138
+ combined = list (zip (list1 , list2 ))
139
+
140
+ # ----------------------------------------------------------------------
141
+ # Creating a Function with Default Arguments:
142
+ def greet (name = "Guest" ):
143
+ # Function code
144
+ pass
145
+
146
+ # ----------------------------------------------------------------------
147
+ # Using the range Function:
148
+ for i in range (5 ):
149
+ # Code that runs 5 times
150
+ pass
151
+
152
+ # ----------------------------------------------------------------------
153
+ # String Formatting with f-strings:
154
+ name = "Alice"
155
+ greeting = f"Hello, { name } !"
156
+
157
+
158
+ # ----------------------------------------------------------------------
159
+ # Using the in Operator with Strings:
160
+ if "substring" in my_string :
161
+ # Code to handle substring presence
162
+ pass
163
+
164
+ # ----------------------------------------------------------------------
165
+ # Checking for None:
166
+ my_variable = None
167
+ if my_variable is None :
168
+ # Code to handle None
169
+ pass
170
+
171
+
172
+ # ----------------------------------------------------------------------
173
+ # Creating and Using Classes:
174
+ class MyClass :
175
+ def __init__ (self ):
176
+ # Constructor code
177
+ pass
178
+
179
+ def my_method (self ):
180
+ # Method code
181
+ pass
182
+
183
+
184
+ # ----------------------------------------------------------------------
185
+ # Working with Dates and Times:
186
+ from datetime import datetime
187
+ now = datetime .now ()
188
+
189
+ # ----------------------------------------------------------------------
190
+ # Using map to Apply a Function to a List:
191
+ doubled = list (map (lambda x : x * 2 , my_list ))
192
+
193
+ # ----------------------------------------------------------------------
194
+ # Using filter to Filter a List:
195
+ even_numbers = list (filter (lambda x : x % 2 == 0 , my_list ))
196
+
197
+ # ----------------------------------------------------------------------
198
+ # Creating a List of Unique Values:
199
+ unique_values = list (set (my_list ))
200
+
201
+ # ----------------------------------------------------------------------
202
+ # Handling Keyboard Interrupt (Ctrl+C):
203
+ try :
204
+ while True :
205
+ # Code that runs indefinitely
206
+ pass
207
+ except KeyboardInterrupt :
208
+ # Code to handle Ctrl+C
209
+ pass
210
+
211
+ # ----------------------------------------------------------------------
212
+ # Working with JSON Data:
213
+ import json
214
+ json_string = '{"key": "value"}'
215
+ data = json .loads (json_string )
216
+
217
+ # ----------------------------------------------------------------------
218
+ # Using itertools for Iteration:
219
+ from itertools import combinations
220
+ combos = combinations (my_list , 2 )
221
+
222
+ # ----------------------------------------------------------------------
223
+ # Creating a Virtual Environment:
224
+ """
225
+ shell command
226
+
227
+ python -m venv myenv
228
+ """
229
+
230
+ # ----------------------------------------------------------------------
231
+ # Activating a Virtual Environment:
232
+ """
233
+ shell command
234
+
235
+ source myenv/bin/activate # On Unix/Linux
236
+ myenv\Scripts\a ctivate # On Windows
237
+ """
238
+
239
+ # ----------------------------------------------------------------------
240
+ # Installing Packages with pip:
241
+
242
+ """
243
+ shell command
244
+
245
+ pip install package_name
246
+ """
247
+
248
+ # ----------------------------------------------------------------------
249
+ # Using a try...except Block with File Operations:
250
+ try :
251
+ with open ('file.txt' , 'r' ) as file :
252
+ content = file .read ()
253
+ except FileNotFoundError :
254
+ # Handle file not found
255
+ pass
256
+
257
+ # ----------------------------------------------------------------------
258
+ # Working with Command-Line Arguments:
259
+ import sys
260
+ arg1 = sys .argv [1 ]
261
+
262
+
263
+ # ----------------------------------------------------------------------
264
+ # Using defaultdict for Default Values in Dictionaries:
265
+ from collections import defaultdict
266
+ my_dict = defaultdict (int )
267
+
268
+ # ----------------------------------------------------------------------
269
+ # Reading CSV Files with csv Module:
270
+ import csv
271
+ with open ('data.csv' , newline = '' ) as csvfile :
272
+ csvreader = csv .reader (csvfile )
273
+ for row in csvreader :
274
+ # Process rows
275
+ pass
276
+
277
+ # ----------------------------------------------------------------------
278
+ # Using Regular Expressions:
279
+ import re
280
+ pattern = r'\d+'
281
+ result = re .findall (pattern , my_string )
282
+
283
+ # ----------------------------------------------------------------------
284
+ # Creating and Using Generators:
285
+ def my_generator ():
286
+ yield 1
287
+ yield 2
288
+
289
+ # ----------------------------------------------------------------------
290
+ # Using os Module for File Operations:
291
+ import os
292
+ file_exists = os .path .exists ('file.txt' )
0 commit comments