Utoljára aktív 1731080194

some examples of good and bad pythonic snippets

kristofer's Avatar kristofer gist felülvizsgálása 1731080193. Revízióhoz ugrás

1 file changed, 7 insertions

ispythonic.md

@@ -137,6 +137,13 @@ from collections import defaultdict
137 137 counts = defaultdict(int)
138 138 for word in words:
139 139 counts[word] += 1
140 +
141 + # OR
142 +
143 + counts = {}
144 + for word in words::
145 + counts[word] = dcounts.get(word, 0) + 1
146 +
140 147 ```
141 148
142 149

kristofer's Avatar kristofer gist felülvizsgálása 1731080096. Revízióhoz ugrás

1 file changed, 9 insertions, 9 deletions

is-pythonic.md fájl átnevezve erre: ispythonic.md

@@ -2,7 +2,7 @@ Pythonic" code refers to code that is written in a way that is idiomatic to Pyth
2 2 taking advantage of its features and conventions to produce clear, concise, and readable code.
3 3 Here are some examples showcasing Pythonic principles:
4 4
5 - *** 1. List Comprehensions
5 + ### 1. List Comprehensions
6 6
7 7 Not Pythonic:
8 8
@@ -17,7 +17,7 @@ Pythonic:
17 17 squares = [x**2 for x in range(10)]
18 18 ```
19 19
20 - === 2. Using enumerate()
20 + ### 2. Using enumerate()
21 21
22 22 Not Pythonic:
23 23
@@ -34,7 +34,7 @@ for index, value in enumerate(my_list):
34 34 print(index, value)
35 35 ```
36 36
37 - === 3. Using zip()
37 + ### 3. Using zip()
38 38
39 39 Not Pythonic:
40 40
@@ -50,7 +50,7 @@ Pythonic:
50 50 combined = list(zip(names, ages))
51 51 ```
52 52
53 - === 4. Using with for File Operations
53 + ### 4. Using with for File Operations
54 54
55 55 Not Pythonic:
56 56
@@ -66,7 +66,7 @@ with open('data.txt', 'r') as file:
66 66 data = file.read()
67 67 ```
68 68
69 - === 5. Using Generators for Efficient Iteration
69 + ### 5. Using Generators for Efficient Iteration
70 70
71 71 Not Pythonic:
72 72
@@ -85,7 +85,7 @@ def get_even_numbers(n):
85 85 return (i for i in range(n) if i % 2 == 0)
86 86 ```
87 87
88 - === 6. Using any() and all()
88 + ### 6. Using any() and all()
89 89
90 90 Not Pythonic:
91 91
@@ -100,7 +100,7 @@ Pythonic:
100 100 has_values = bool(my_list)
101 101 ```
102 102
103 - === 7. Conditional Expressions (Ternary Operator)
103 + ### 7. Conditional Expressions (Ternary Operator)
104 104
105 105 Not Pythonic:
106 106
@@ -116,7 +116,7 @@ Pythonic:
116 116 result = 'Yes' if condition else 'No'
117 117 ```
118 118
119 - === 8. Leveraging Default Dictionary
119 + ### 8. Leveraging Default Dictionary
120 120
121 121 Not Pythonic:
122 122
@@ -140,7 +140,7 @@ for word in words:
140 140 ```
141 141
142 142
143 - === Conclusion
143 + ### Conclusion
144 144
145 145 Pythonic code emphasizes readability and efficiency, often leveraging built-in functions and
146 146 language features to reduce boilerplate code.

kristofer's Avatar kristofer gist felülvizsgálása 1731080017. Revízióhoz ugrás

1 file changed, 1 insertion, 1 deletion

is-pythonic.md

@@ -2,7 +2,7 @@ Pythonic" code refers to code that is written in a way that is idiomatic to Pyth
2 2 taking advantage of its features and conventions to produce clear, concise, and readable code.
3 3 Here are some examples showcasing Pythonic principles:
4 4
5 - === 1. List Comprehensions
5 + *** 1. List Comprehensions
6 6
7 7 Not Pythonic:
8 8

kristofer's Avatar kristofer gist felülvizsgálása 1731079972. Revízióhoz ugrás

1 file changed, 148 insertions

is-pythonic.md(fájl létrehozva)

@@ -0,0 +1,148 @@
1 + Pythonic" code refers to code that is written in a way that is idiomatic to Python,
2 + taking advantage of its features and conventions to produce clear, concise, and readable code.
3 + Here are some examples showcasing Pythonic principles:
4 +
5 + === 1. List Comprehensions
6 +
7 + Not Pythonic:
8 +
9 + ```python
10 + squares = []
11 + for x in range(10):
12 + squares.append(x**2)
13 + ```
14 +
15 + Pythonic:
16 + ```python
17 + squares = [x**2 for x in range(10)]
18 + ```
19 +
20 + === 2. Using enumerate()
21 +
22 + Not Pythonic:
23 +
24 + ```python
25 + index = 0
26 + for value in my_list:
27 + print(index, value)
28 + index += 1
29 + ```
30 + Pythonic:
31 +
32 + ```python
33 + for index, value in enumerate(my_list):
34 + print(index, value)
35 + ```
36 +
37 + === 3. Using zip()
38 +
39 + Not Pythonic:
40 +
41 + ```python
42 + names = ['Alice', 'Bob', 'Charlie']
43 + ages = [24, 30, 22]
44 + combined = []
45 + for i in range(len(names)):
46 + combined.append((names[i], ages[i]))
47 + ```
48 + Pythonic:
49 + ```python
50 + combined = list(zip(names, ages))
51 + ```
52 +
53 + === 4. Using with for File Operations
54 +
55 + Not Pythonic:
56 +
57 + ```python
58 + file = open('data.txt', 'r')
59 + data = file.read()
60 + file.close()
61 + ```
62 + Pythonic:
63 +
64 + ```python
65 + with open('data.txt', 'r') as file:
66 + data = file.read()
67 + ```
68 +
69 + === 5. Using Generators for Efficient Iteration
70 +
71 + Not Pythonic:
72 +
73 + ```python
74 + def get_even_numbers(n):
75 + even_numbers = []
76 + for i in range(n):
77 + if i % 2 == 0:
78 + even_numbers.append(i)
79 + return even_numbers
80 + ```
81 + Pythonic:
82 +
83 + ```python
84 + def get_even_numbers(n):
85 + return (i for i in range(n) if i % 2 == 0)
86 + ```
87 +
88 + === 6. Using any() and all()
89 +
90 + Not Pythonic:
91 +
92 + ```python
93 + if len(my_list) > 0:
94 + has_values = True
95 + else:
96 + has_values = False
97 + ```
98 + Pythonic:
99 + ```python
100 + has_values = bool(my_list)
101 + ```
102 +
103 + === 7. Conditional Expressions (Ternary Operator)
104 +
105 + Not Pythonic:
106 +
107 + ```python
108 + if condition:
109 + result = 'Yes'
110 + else:
111 + result = 'No'
112 + ```
113 +
114 + Pythonic:
115 + ```python
116 + result = 'Yes' if condition else 'No'
117 + ```
118 +
119 + === 8. Leveraging Default Dictionary
120 +
121 + Not Pythonic:
122 +
123 + ```python
124 + counts = {}
125 + for word in words:
126 + if word in counts:
127 + counts[word] += 1
128 + else:
129 + counts[word] = 1
130 + ```
131 +
132 + Pythonic:
133 +
134 + ```python
135 + from collections import defaultdict
136 +
137 + counts = defaultdict(int)
138 + for word in words:
139 + counts[word] += 1
140 + ```
141 +
142 +
143 + === Conclusion
144 +
145 + Pythonic code emphasizes readability and efficiency, often leveraging built-in functions and
146 + language features to reduce boilerplate code.
147 + By adopting these idioms, you can write code that is not only functional but also
148 + elegant and easy to understand.
Újabb Régebbi