Python Forum
Quick tips and tricks for python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quick tips and tricks for python
#1
What are some easy and quick tricks that you learned through coding that a beginner like myself should be aware of to not have to make easy mistakes?
Reply
#2
Tip: Don't worry about making mistakes, you learn from them.
Reply
#3
As Yoriz said - mistakes are good. Correcting mistakes is most effective learning process Smile

One should consider general approach as well. Python is programming language (emphasis on language). What do you do if you learn a (spoken) language? You learn vocabulary, punctuation, tenses etc. What is the ultimate purpose of learning language? To express yourself to those who speak that language. In order to do so you need two things: something to say and knowledge how to say it in that language.

Programming is all about solving problems/tasks. Therefore 'something to say' in terms of programming is idea (a.k.a algorithm) how to solve particular problem. You need to have this idea in spoken language (or as flowchart, scribble etc) and only then can you implement it in programming language. So 'nothing to say, nothing to code'.

To whom you 'speak'? It may tempting to say that to computer. However you 'speak' to your fellow programmers as well. It is important that your idea is expressed concisely and in readable way (Python Zen describes how good code should look like). Computer doesn't care if your code is mess - if it's syntax is correct and has no errors it will run happily whatever you write. But it is said that developer spends 80% of time reading others code and 20% time writing code.

As practical advice I suggest to implement your own 'wiki' (whether it Jupyter Notebook or Github gists/project). In said wiki you could write down solutions for generalised problems. Like: 'how to get second largest element in list/tuple', 'how to get dictionary key which has largest value' etc. This serves two purposes - you learn to define problems in generalised way and look for solution in abstract way (not to mention that ability to define problem will significantly improve your search results in SO or in this forum). There is subtle difference in "I need to count letters in 'hello world'" and 'how to count sequence elements'

Happy coding!

EDIT (some additional tidbits):

Python interactive interpreter is very useful while learning. Lot of information is at your fingertips, for example help. You can study what function does instead of going to web and search there.

If you start learning there is lot of printing to screen so you should master it:

>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
(END)


As you can see you can set separator, end, print to file and flush the stream. You should try them all and find out what they actually do:

>>> print('a', 'b')
a b
>>> print('a', 'b', sep='|')
a|b
>>> print('a', 'b', sep='|', end='=')
a|b=>>> 
There is also unpacking what can be used in printing:

>>> print(['a', 'b'])
['a', 'b']
>>> print(*['a', 'b'])
a b
>>> print(*['a', 'b'], sep=', ')
a, b
>>> print(*'hello')
h e l l o
>>> print(*'hello', sep='-')
h-e-l-l-o
>>> print({1: 'one', 2: 'two'})
{1: 'one', 2: 'two'}
>>> print(*{1: 'one', 2: 'two'})
1 2
>>> print(*{1: 'one', 2: 'two'}.values())
one two
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner/Intermediate Tips Ex_database 1 2,049 Sep-27-2019, 04:24 PM
Last Post: Larz60+
  print: Tips and tricks microphone_head 12 5,500 Apr-12-2019, 04:03 PM
Last Post: microphone_head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020