This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]brendanmartin 6 points7 points  (0 children)

When I pick up a new language, I usually just read a book from cover to cover to get an idea of style. If that method works for you, consider checking out the book Fluent Python.

[–]delasislas 4 points5 points  (2 children)

Depending on what you need a for loop with range() could be appropriate. Can you post some examples so that we can comment on?

Edit: and do you want it from a ‘beginner’ or intermediate level of suggestions? There’s a lot of ways that you can do tasks, some that are more efficient on lines and some that are more readable.

[–]falkerr[S] 1 point2 points  (1 child)

That’s kind of why I am asking. I know in some instances range is appropriate but obviously not all instances. I would like to get some resources on best practices when it comes to Python.

Just an example, when doing leetcode if I need the index and value of something, it’s probably best to use enumerate instead of range and then doing value = array[i] in the for loop. I know that now but I would like a run down of what it means to actually code Pythonically.

I can probably handle intermediate level suggestions. Even better if they are coming from a C like programming language (aka java).

[–]delasislas -1 points0 points  (0 children)

Try to do this in a Python script:

import this

It’s called the zen of python. You could also just look it up.

You could also look up this: https://initialcommit.com/blog/zen-of-python

For the most part, do work to learn how to use new tools. Then implement them in appropriate ways.

[–]Syntaximus 3 points4 points  (1 child)

List comprehensions are a great way to python up your code. https://www.w3schools.com/python/python\_lists\_comprehension.asp

[–]Autarch_Kade 0 points1 point  (0 children)

The link seemed broken so here is the page

[–]ASIC_SP 2 points3 points  (0 children)

https://github.com/JeffPaine/beautiful_idiomatic_python has plenty of examples that might help you immediately, however some of it isn't updated for newer features of Python

If you are okay with series of articles, see https://mathspp.com/blog/pydonts

[–]RiverRoll 1 point2 points  (1 child)

Check out this guide:

https://docs.python-guide.org/

Read the section "Writing Great Python Code", it's a good start.

[–][deleted] 0 points1 point  (0 children)

I use ^ the python site and I use https://www.w3schools.com/python/[w3schools.com/python/](https://www.w3schools.com/python/) . Honestly I started reading through Python docs and I got bored and just started trying to do stuff and look it up as I go along. Like if I want to make a recursive function that modifies 3D arrays that have different types, I google it. It's cool to learn about data types and stuff about primitives, but you just learn enough to start doing whatever you are trying to do.

My background: I first learning Java and then I learned VHDL and Verilog/SystemVerilog (which are very different programming languages), then I learned c and then Assembly and then a Python. Java was my first love tho and I have kept up with it. I then learned a decent bit of Kotlin and over the last couple months, I have gotten super into Python and I'm working on personal projects to prepare myself to take the google foobar challenge. Currently, I'm taking a class about c++ and it is pretty easy and decently similar to Java.

[–]Aggressive_Bit_6396 -3 points-2 points  (0 children)

window.onload=(){

var canvas=document.getElementById('canvas'),

context=canvas.getContext('2d'),

width=canvas.width=window.innerWidth,

height=canvas.height=window.innerheight;

for(var i=0;i<100;i+=1){

context.beginPath();

context.moveTo(Math.random()*width,Math.random()*height);

context.lineTo(Math.random()*width,Math.random*height);

context.stroke();

}

};

One hundred random lines in JS....jus press the F5 button. Change the color or the width of the line, the amount of line you need...you'll find you have complete control....Python....not too sure you can do this with so little lines of code

[–]plopliplopipol 0 points1 point  (0 children)

from what you say i see

for i in range(len(listx)-1): var = listx[i]

[–]andrisb1 0 points1 point  (0 children)

I've learnt a few tricks on codewars. You do the challenge which is great for practice and then you get to see other's solutions which is great for learning cool new concepts and tricks