Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
Python for newcomers part 1
  • Well welcome to my new series. Python for newcomers. Since I'm pretty much finished with my C for newcomers series, I've decided to write up a Python for newcomers series. Hopefully this will help many people with Python and help get started with it.

    Part 1 - Getting started

    Python is a now widely known language. I prefer it over many languages for its functionality and its power. With such a great community theres so many people willing to help you, and theres so many new modules being created each day. Hopefully this series will help you learn the joy of this programming language. Python is great for many things, but not the best when you need to interface with computer hardware and such because python is interpreted. This means it's slower than a compiled language such as C. Python is also a high level language, meaning it uses layers to communicate with the computers OS // Hardware.

    Installing
    Python can be almost any system. If you're running a *nix box, you can install it with your preferred package manager. Of course, you can download Python from http://python.org/

    Once you've got it installed you should be all set up and ready to go.

    Modes
    There are two main ways to interact with python. One is Interactive mode and the other is IDLE. Interactive mode is like a command line. You type instructions one line at a time. IDLE includes Pythons interactive mode and much more. To run interactive mode, just type python at your command line. It should say something like this:

    Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
    [GCC 4.4.3] on linux2
    Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
    >>>

    This is interactive mode(Distinguished by the >>>). You can really do anything in interactive mode that you'd usually do in a python program. You can import, create functions, classes, etc. By default, interactive mode will print to the screen.

    Hello world
    Now it's about time to create our first ever program in python! This is easy. First open up interactive mode and get the prompt with the three >'s Then type the following:

    print \"Hello, World!\"

    And hit enter. You should get the output Hello, World. Then it will go back to the >>>.

    Now how about values? If you're familiar with programming you should know what a value is. Take a look at the following code:

    x = 2
    print x

    The first line assigns a value to x. This value is set as two. Then we print whatever the value of x is. We can change x to anything we want. A list, tuple, string, integer or whatever.

    Manipulating strings
    In Python we can assign strings to lets say, x. Then we can easily print it.

    x = \"Hello\"
    print x

    Easy. You can add strings together too.

    x = \"Hello \"
    y = \"World\"
    print x + y

    You should get the output Hello World. You can add together strings, numbers, lists, tuples and tons of other things.

    Python calculator
    Python, like all other languages, is good for doing math. You can add, subtract, multiply, divide and much more. For more math options you can import the math module. I will not be going over the math module just yet. Here is a list of basic operators:
    + Addition x + y
    - Subtraction x - y
    * Multiplication x * y
    / Division x / y

    Don't use an equals sign when preforming math. The equals sign is for assigning a name to a value. If you try to use it like so:
    2 + 2 =

    You will get a syntax error.

    Imports
    Importing modules is an important thing in Python. Importing modules provide extra code other than the defaults included such as print. To import a module, type Import followed by the module. Let's say I want to import the math module and print the value of pi.

    import math
    x = math.pi()
    print x

    This will print the value of pi. You can import several modules in one python code, such as sys, os, math and more.

    Comments
    Comments are very important in Python(Or in any other programming languages). Comments can help increase readability of code. If others are reading your code, they can read your comments to see what is going on in the code. You can also include comments to help you remember what you were doing or what you need to do in the code.
    To create a comment, just add a #(pound) symbol. Anything following a pound will not be included in the code.

    print \"Hi\" #Prints Hi to the screen

    Note: Do not add comments to everything. That's just retarded. Add comments to things that need them.

    Getting Help
    Python includes a built in help function. This is very useful when you can't remember how to use a specific function or what. To use this help function, open the interactive mode and type help(). You should get this prompt:
    help>

    From here you can type a module, keyword or topic and get help about it. You can also type help(module) to get help about a specific module.


    I hope you liked this very simple introduction to Python. Stay tuned for part 2, where I will discuss basic use of Strings, Integers and lists.

    --Chroniccommand
  • Xin
    Posts: 3,251
    Great guide chronic :)
    Xin
  • undead
    Posts: 822
    Nice guide ;)
  • Sh3llc0d3
    Posts: 1,910
    Oh my dead god!

    Don't think I need to say anymore lol.
  • Xin
    Posts: 3,251
    said:


    Oh my dead god!

    Don't think I need to say anymore lol.



    hes at it again!!!!!!!!!!!!!!
    Xin
  • chroniccommand
    Posts: 1,389
    Lmao. Just Lmao.
  • Sh3llc0d3
    Posts: 1,910
    I just had to laugh i really couldn't believe it lol
  • Wow, this was really cool! Thank you for this awesome Tutorial! :)
    ~pandas