Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Python Tutorial - The Basics

DZone's Guide to

Python Tutorial - The Basics

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

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 is a dynamically typed and strongly typed programming language.

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:

print "Hello"

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.

Conclusion

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.



Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}