Python/Best Tips to Increase Your Productivity in Python.md
... ...
@@ -0,0 +1,172 @@
1
+https://medium.com/@pranjalgoyal13/best-tips-to-increase-your-productivity-in-python-856fb038a676
2
+
3
+# Best Tips to Increase Your Productivity in Python | by Pranjal Goyal | Medium
4
+
5
+![Pranjal Goyal](https://miro.medium.com/v2/resize:fill:88:88/1*0Q69xumBtjkIW6Jjw02HPA.jpeg)
6
+[Pranjal Goyal](https://medium.com/@pranjalgoyal13)
7
+
8
+Python provides many inbuilt functionalities which help in writing cleaner code. People are generally unaware of some productive tips that are used in Python to improve the quality and readability of code. πŸ‘©β€πŸ’»
9
+
10
+> Let’s make your code more Pythonic…
11
+
12
+Cool Logo Right?
13
+
14
+Best Productive Tips are as follows:
15
+------------------------------------
16
+
17
+Using List Comprehension:
18
+-------------------------
19
+
20
+Suppose you want to add elements to a list from an existing one, the conventional way is to loop through the list and then append the values to the new list.
21
+
22
+But Python offers a convenient way to handle List creation as well as putting values in it. It looks cleaner because of the shorter syntax and by using it we can get rid of the complex blocks of code with the utility of a single for loop within a list.
23
+
24
+**Example:**
25
+
26
+**Conventional Way** πŸ™‚**:**
27
+
28
+```
29
+students = ['Sam', 'Billy', 'John', 'Lucas']
30
+new_students = []
31
+for student in students:
32
+ new_students.append(student)
33
+print(new_students)
34
+```
35
+
36
+
37
+Python List
38
+
39
+**Python Way πŸ”₯:**
40
+
41
+```
42
+students = ['Sam', 'Billy', 'John', 'Lucas']
43
+new_students = [student for student in students]
44
+print(new_students)
45
+```
46
+
47
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*bhTeVP1MKpvBrRUqTTKQyw.jpeg)
48
+Python List
49
+
50
+Making strings using F-Strings:
51
+-------------------------------
52
+
53
+Not the word that you are thinking of πŸ˜…. F-String was introduced in Python to make string interpolation simpler.
54
+
55
+This is used to avoid string concatenation and string conversion which is still used by many to print variables in a string.
56
+
57
+The reason **fstring** is _better_ in formatting than the **_%s_** or **.format()** method is the readability and simplicity it provides (when strings get very large with increasing variables) as compared to other known methods.
58
+
59
+To create an f-string, just append the string with **f**. Let us look at an **example.**
60
+
61
+**Conventional Way**πŸ™‚**:**
62
+
63
+```
64
+name = 'John Doe'
65
+age = 21
66
+gender = 'Male'
67
+print("The name is " + name + " age: " + str(age) + " gender: " + gender)
68
+#or
69
+print("The name is %s age: %s and gender: %s" % (name, age, gender))
70
+```
71
+
72
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*XAWu5KGlLEqxXQzpzCGf_Q.jpeg)
73
+Printing string in python conventional way
74
+
75
+**Python Way πŸ’―:**
76
+
77
+```
78
+name = 'John Doe'
79
+age = 21
80
+gender = 'Male'
81
+print(f'The name is {name} age: {age} and gender:{gender}')
82
+```
83
+
84
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*0qjIb4gNWC2yfB2guVQwbw.jpeg)
85
+F String in Python
86
+
87
+Enumerate Looping:
88
+------------------
89
+
90
+To track both index and value while iterating a list, the conventional **range()** function is used.
91
+
92
+But there is a better way in Python to loop a list using **enumerate()** function.
93
+
94
+**Example:**
95
+
96
+**Conventional Way😢:**
97
+
98
+```
99
+students = ['Sam', 'Billy', 'John', 'Lucas']
100
+for i in range(len(students)):
101
+ print(f'The index : {i}, Name: {students[i]}')
102
+```
103
+
104
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*aOB-VtNab9u34-7eI1W5HQ.jpeg)
105
+Looping Index-Based
106
+
107
+**Python Way πŸš€:**
108
+
109
+```
110
+students = ['Sam', 'Billy', 'John', 'Lucas']
111
+for index, student in enumerate(students):
112
+ print(f'The index : {index}, Name: {student}')
113
+```
114
+
115
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*ENDZjq20x_wvuTTxJG4diQ.jpeg)
116
+Looping with enumerating
117
+
118
+Using Generators:
119
+-----------------
120
+
121
+It is a more memory-efficient way to store and return data only when it is demanded (lazy loading). Let's say you want to sum a large range of numbers, so it will be a better choice to use the generator.
122
+
123
+To use a generator use **()** around the list instead of **\[\]**.
124
+
125
+**Example:**
126
+
127
+**Conventional Way**πŸ™‚**:**
128
+
129
+```
130
+get_range_sum = [num for num in range(100000)]
131
+print(sum(get_range_sum))
132
+```
133
+![](https://miro.medium.com/v2/resize:fit:640/format:webp/1*fb50bb5JesV3_L_WPuBx2g.jpeg)
134
+
135
+Without Generation
136
+
137
+**Python Way πŸ’Ύ:**
138
+
139
+```
140
+get_range_sum = (num for num in range(100000))
141
+print(sum(get_range_sum))
142
+```
143
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*GRgjaEfrQPEmt-OgZZCmDg.jpeg)
144
+
145
+With Generation
146
+
147
+**_Below are productive hacks that apply to every language_**
148
+
149
+Exception Handling:
150
+-------------------
151
+
152
+Using Exception handling is very useful as it doesn't stop your code when an error occurs and provides an alternate route for it.
153
+
154
+If the exception is not handled the program will abruptly terminate which can lead to various handling issues.
155
+
156
+There are mainly two times of exceptions:
157
+
158
+* **Checked Exception:** Occurs at compile time and the code will not compile unless it is handled.
159
+* **Unchecked Exception:** Occurs at Run-time, the most famous example is DivisonByZeroError.
160
+
161
+I have written a dedicated [Medium Article](https://medium.com/codex/exception-handling-an-art-of-try-and-catch-5e85d4af2bfc?source=post_page-----856fb038a676--------------------------------) on Exception Handling which covers in detail about this.
162
+
163
+Finding bugs using Debugging:
164
+-----------------------------
165
+
166
+To find what went wrong with the code, debugging can help to resolve the issue much faster.
167
+
168
+Conventionally people use **print()** statements to debug the slower code as well will become difficult as the code base grows.
169
+
170
+In debugging we use _breakpoints_ to debug applications which will pause the execution of the program at that point and all variables, and stack track is visible in the debugger console.
171
+
172
+I have written a dedicated [Medium Article](https://medium.com/@pranjalgoyal13/the-art-of-debugging-basics-a40802d52ece?source=post_page-----856fb038a676--------------------------------) on _Basics of Debugging_.