+ 4
what's the commonly used conception when coding in "python" way
i know how to code in python, but not in python-ish way. i've been coding in c-like language since i know how to write a program, its kinda hard for me to learn something different like python. so. can anyone tell me the commonly used concept like list comperhension ? and maybe when and how to use them. thx
11 Antworten
+ 5
Conventions in Python value readability. You write code following a specific set of rules (ones that you think make it readable) and stick to it
For example:
- Tabs are 4 spaces
- Functions are Pascal Case (HelloWorld)
- Variables are Snake Case(hello_world)
- comments are above lines of code
- 2 lines of space below every function definition
These are all style conventions I stick to
+ 4
It's just a matter of different syntaxes and some extra concepts like object oriented programming which will make your code efficient. There's nothing hard in python. Just try and practise daily. Even I'm shifting from c++ to python.
+ 3
Python has a lot of tools that make it unnecessary in many cases to write code. I think it's important to use all that capital (and if you just learn the details of the language, you'll probably voluntarily do that).
Beyond that - readability.
Here's a summary how to achieve that in a sort of canonical way:
https://www.python.org/dev/peps/pep-0008/
+ 1
List comprehentions build lists using for loops.
[c for c in "Hello"]
>>> ['H', 'e', 'l', 'l', 'o']
+ 1
They can also be used to "filter" values in a list.
all_nums = [1, 2, 3, 4, 5, 6, 7, 8]
even_nums = [i for i in all_nums if i%2 == 0]
print(*even_nums)
>>> 2 4 6 8
+ 1
They also make a list, by "slicing" pieces of another one
Syntax of a list slice:
list[start: end: step]
+ 1
If your target is writing a readable code, i encourage you to read pep8 and pep20 also known as zen of python, which will guide you to coding in a more pythonic way.
+ 1
Thomas Williams PEP recommends snake_case for variable and function names too (except when backwards compatibility needed for historical reason where camelCase was used in old code)
Class names are supposed to be in CapWords style.
0
what about other concept like slicing and stuff ?
0
You can message me if you have any more questions👍🏼
0
last one, any other things that commonly used like you just explain above ?