background preloader

Python

Facebook Twitter

Beginner's Resources. ForLoop. Usage in Python When do I use for loops? For loops are traditionally used when you have a piece of code which you want to repeat n number of times. As an alternative, there is the WhileLoop, however, while is used when a condition is to be met, or if you want a piece of code to repeat forever, for example - For loop from 0 to 2, therefore running 3 times. for x in range(0, 3): print "We're on time %d" % (x) While loop from 1 to infinity, therefore running infinity times. x = 1 while True: print "To infinity and beyond!

As you can see, they serve different purposes. How do they work? If you've done any programming before, there's no doubt you've come across a for loop or an equivalent to it. Nested loops When you have a piece of code you want to run x number of times, then code within that code which you want to run y number of times, you use what is known as a "nested loop". Early exits Like the while loop, the for loop can be made to exit before the given object is finished.

The "Invent with Python" Blog — “How much math do I need to know to program?” Not That Much, Actually. Here are some posts I’ve seen on the r/learnprogramming subreddit forum: Math and programming have a somewhat misunderstood relationship.

The "Invent with Python" Blog — “How much math do I need to know to program?” Not That Much, Actually.

Many people think that you have to be good at math or made good grades in math class before you can even begin to learn programming. But how much math does a person need to know in order to program? Not that much actually. This article will go into detail about the kinds of math you should know for programming. For general programming, you should know the following: Addition, subtraction, division, and multiplication – And really, the computer will be doing the adding, subtracting, dividing, and multiplying for you anyway. The "Invent with Python" Blog — Stop Using “print” for Debugging: A 5 Minute Quickstart Guide to Python’s logging Module. This tutorial is short.To figure out bugs in your code, you might put in print statements/print() calls to display the value of variables.Don’t do this.

The "Invent with Python" Blog — Stop Using “print” for Debugging: A 5 Minute Quickstart Guide to Python’s logging Module

Use the Python logging module. The logging is better than printing because: It’s easy to put a timestamp in each message, which is very handy.You can have different levels of urgency for messages, and filter out less urgent messages.When you want to later find/remove log messages, you won’t get them confused for real print() calls.If you just print to a log file, it’s easy to leave the log function calls in and just ignore them when you don’t need them. (You don’t have to constantly pull out print() calls.) Using print is for coders with too much time on their hands.

To print log messages to the screen, copy and paste this code: import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') logging.debug('This is a log message.') The tail command comes on Mac OS X and Linux OSes.

Videos

Texts. Assignments/Challenges. Looking for the best solutions. Popular Python recipes. Python. For Beginners. Welcome! Are you completely new to programming? If not then we presume you will be looking for information about why and how to get started with Python. Fortunately an experienced programmer in any programming language (whatever it may be) can pick up Python very quickly.

It's also easy for beginners to use and learn, so jump in! Installing Python is generally easy, and nowadays many Linux and UNIX distributions include a recent Python. If you want to know whether a particular application, or a library with particular functionality, is available in Python there are a number of possible sources of information. If you have a question, it's a good idea to try the FAQ, which answers the most commonly asked questions about Python.

Python 3.X Documentation

Python 2.X Documentation.