+ 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

12th Jul 2019, 7:17 AM
Taste
Taste - avatar
11 odpowiedzi
+ 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
12th Jul 2019, 8:01 AM
Trigger
Trigger - avatar
+ 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.
13th Jul 2019, 11:02 AM
Manoj
Manoj - avatar
+ 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/
12th Jul 2019, 9:08 AM
HonFu
HonFu - avatar
+ 1
List comprehentions build lists using for loops. [c for c in "Hello"] >>> ['H', 'e', 'l', 'l', 'o']
12th Jul 2019, 8:03 AM
Trigger
Trigger - avatar
+ 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
12th Jul 2019, 8:06 AM
Trigger
Trigger - avatar
+ 1
They also make a list, by "slicing" pieces of another one Syntax of a list slice: list[start: end: step]
12th Jul 2019, 8:18 AM
Trigger
Trigger - avatar
+ 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.
12th Jul 2019, 9:47 AM
Mo Hani
Mo Hani - avatar
+ 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.
12th Jul 2019, 10:34 AM
Tibor Santa
Tibor Santa - avatar
0
what about other concept like slicing and stuff ?
12th Jul 2019, 8:16 AM
Taste
Taste - avatar
0
You can message me if you have any more questions👍🏼
12th Jul 2019, 8:19 AM
Trigger
Trigger - avatar
0
last one, any other things that commonly used like you just explain above ?
12th Jul 2019, 8:20 AM
Taste
Taste - avatar