Python Tutorial - The Basics
Python Tutorial - The Basics
Join the DZone community and get the full member experience.Join For Free
Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.
I have wanted to try Python, and now I've had enough time to try it out. My goal was to learn why it is so popular and widely used. I started out with Django, but soon find out that it was too much. I needed to learn the basics of Python first.
I found Dive Into Python, which is a tutorial for Python 2.x. There is already Python 3.x, but the majority of programs and libraries work only with 2.x.
Python Programming Language
Python is an interpreted, high-level and very readable programming language. It supports object-oriented, functional and imperative programming. It does not include curly brackets to indicate scope. Python uses indentation to separate code blocks. The language rejects Perl the philosophy: "there is more than one way to do it" in favor of "there should be one—and preferably only one—obvious way to do it".
Python implementations come with interactive REPL which is an excellent way to experiment with the language's features. Just invoke a Python executable without any parameters and REPL will fire up itself. If you want more than instant executions, you should try out some IDE. I used JetBrains PyCharm 30-day evaluation version.
First Python Program
Python is very simple to learn and write. Here's an example program:
That's it. You can execute that in REPL or write a snip to file that has a filename extension .py. Run by executing python hello.py or run inside PyCharm.
Defining a Function
Defining a single function without any Class definitions is a no-brainer:
def multiplyByTwoAndPrint(number): """ Multiply given number by two and print the result """ print number * 2 print multiplyByTwoAndPrint(10) print multiplyByTwoAndPrint.__doc__
Every function starts with a keyword def fallowed by name of the function and argument(s) in parentheses. Multiple arguments are separated with commas. Function definitions end with a colon. In Python you don't specify return type. Every function in Python returns a value and if there's no return keyword defined, None is returned. None is Python's null.
Remember to indent your code correctly!
Functions can define comments that begin and end with """. It's sort of javadoc, but you can access it in run-time using the built-in __doc__ function.
Example above will print:
20 None Multply given number by two and print the result
First line is printed by our newly defined function. The second line will print None because we did not return anything. Python will return None for us. The third line is the docstring of the function.
Python is a very easy language to start programming with. It holds very powerful ways to manipulate data, which are essential for a good programming language.
This was the first part of my Python tutorial. In the next part I will introduce Dictionaries, Lists and ways to manipulate them.
Opinions expressed by DZone contributors are their own.