DEV Community

Rattanak Chea
Rattanak Chea

Posted on

15 6

Python tips 101

I have background with mostly JavaScript and a strong type language such as Java.
Because Python's popularity in Machine learning, AI, data science, as well as backend web development, I decided to really learn it. I have observed that it has very elegant, and clean syntax which enforces us to think of the code on a high level (not too worried about the little detail) and to format code nicely with its strict indentation rule.

Below is some Python and JavaScript's code snippets to illustrate some common usages and language comparison.

# Slicing
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list[3:] # [4, 5, 6.., 10] from the 3th element
list[:3] # [1, 2, 3] first three
# negative index count from the right, starting with -1
list[-3:] # [8, 9, 10] last three
list[3:-3] # [4,5,6,7] elements between 3rd from left and 3rd from right
# list[start:end:n]
# start is 0 by default
# end is len of the list by default
# every nth element
list[::3] # [1, 4, 7, 10]
# reverse list
list[::-1] #create a shallow copy
# list.reverse() $ reverse in place
list_reference = list
list_copy = list[:]
## tricks
a, b = 5, 10
# swapping
a, b = b, a # a =10, b = 5
# array initialization
list2 = [0] * 3 # [0, 0, 0]
matrix = [[0 for x in range(3)] for y in range(2)] # [[0, 0, 0], [0, 0, 0]]
# functional
# filter
even_list = [x for x in list if x % 2 == 0] # [2, 4, 6, 8, 10]
sum(list) #55
max(list) #10
view raw 1. python3 tips hosted with ❤ by GitHub
let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// syntax arr.slice([begin[, end]])
list.slice(3) //[4,5,6, ..10]
list.slice(0, 3) //[1,2,3]
list.slice(-3) // [8, 9, 10] begin from last 3 elements
list.slice(3, -3) // [4,5,6,7]
// reverse list in-place
list.reverse()
// swapping require using a temp element or some hack
// in ES6, you can use destructuring assigment
let a, b
[a, b] = [5, 10];
[a, b] = [b, a];
// array initialization
list2 = [];
// or you can use old for loop
// or Array fill
list3 = new Array(5).fill(0) //[0,0,0,0,0]
// functional
let even_list = list.filter(i => i%2 == 0) //[ 2, 4, 6, 8, 10 ]
let sum = list.reduce((sum, x) => sum + x); //55
let max = Math.max(...list) //10, by deconstructuring array
let max2 = list.reduce(function(a, b) {
return Math.max(a, b);
});

I hope to write more advanced Python topics (I need to learn them first :-p) in future post:

  • [] generators
  • [] iterators
  • [] decorators

If you have any comments and tips, please write below.

For side-by-side code comparison: https://leetcode.com/playground/vDy2x7Co/shared

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (5)

Collapse
 
adityavarma1234 profile image
Aditya Varma

I am really looking forward to more content from you. I am from ruby on rails background and recently switched to python background recently.

In two months I learnt the following, ipython shell which is far better than default python shell that I have been using. It has function name completions, commands repeat to name a few of its features.

One thing I loved the most is that I could look into the doc string in the shell itself by simply appending ? to the end of the function name. Example lets say I have a class test and there is an instance function run. In the console I simply type test.run? and I get the doc string for that. I find it pretty cool and amazing.

Do you have any such tips that you can share with us?

Collapse
 
rattanakchea profile image
Rattanak Chea

Thanks for sharing the tips. They are useful to know.

If you are curious about the language itself, there are many good resources online. Here are a few:
book.pythontips.com/en/latest/
hackernoon.com/python-tricks-101-2...

But it seems like you are curious about the work flow and tools to make development easier.
I use VS code and install python extension which allows me to run and debug python code easily.

Collapse
 
adityavarma1234 profile image
Aditya Varma

Thanks for sharing the links.

Collapse
 
vdedodev profile image
Vincent Dedo

list[::3] # [1, 3, 5, 7, 9]

That should be list[::2] btw :)

Collapse
 
rattanakchea profile image
Rattanak Chea

@V Dedo, nice catch. I updated the code. Thanks.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more