Learning Python from a C-style language background

Post all your tuts or request for tuts here.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Learning Python from a C-style language background

Post by mattykins »

A quick search through this forum showed that no Python tutorial has yet been made. The hardest thing for me about learning python was getting over the initial differences between it and other languages. So this tutorial aims to help transition you from a background of C-style languages to Python!

This tutorial uses Python 2.7.2, NOT 3+. 2.7.2 is much more widely used and many more modules support 2.7.2, although most of the knowledge in this tutorial is universal.

This tutorial assumes no previous Python experience but does assume basic programming knowledge( IE What a data type is )

Lets start with the basic program: Hello World!

Code: Select all

print "Hello world!"
There are 2 things you may be surprised at first off:

1. There is no semicolon at the end of that line of code!
Python has very different syntax from other language such as C++ or Java, for one thing you don't need to end every line with a semi colon. The end of a line is represented by a literal line break (Pressing the enter key)

2. There is no 'main' entry point function or anything to represent the beginning of the code!
In python you do not need a main function, the interpreter will read the program from top to bottom. However i do highly recommend containing almost all of your code within functions and classes and having a main function as it will make your life easier, while also making the code a lot easier to read. But we will get to functions in a little while

Lets move on to another peculiar part of Python, its variables.

Code: Select all

x = 5
y = 10.5
z = x + y
print z
At this point your probably thinking to yourself, "Woh woh woh, hold up here. This guy crazy.. He didn't declare no data type or nuthin'!"
No-no trust me I didn't make a mistake! Python handles all of that for you. When declaring a variable if you give it an integer value it will automagically make that variable an integer. Same thing happens if you assign that variable a float or a double. That automatically becomes a float or double! And if you try to add an integer to a double and slap that value to a new variable, guess what? That new variables data type becomes a double. There are some more advanced situations where you may have to manually set a data type, but Python has some built in functions in the standard library to make that very easy.

Overview of this code:
We assigned an integer value of 5 to x
We assigned a float valu]e of 10.5 to y
We assigned the sum of x and y to a variable z, which in turn became a float because Python handles that for you
Lastly we printed the variable z to the console

Moving on to functions and pythons signature syntax, Tab spacing!

Code: Select all

def printSomething(s):
    print s

printSomething("Hello World!")
The output of this code is the exact same as the first program we covered together. But its in a much more fun and exciting form now! :)
Lets break this one down line by line:
Line 1: the 'def' stands for define. It is used when defining functions.(synonymous to methods in other languages) The word 'printSomething' is arbitrary, it is just the name of the function. You could put 'FlarboJangleMaNangleFoShizzleMyNizzle_FerRizzle' here if you so desired. Although I don't recommend it ;) After that is the set of parenthesis containing the variable name 's'. These are just your parameters for the function. Again the variable name 's' is arbitrary, you could put anything here. If you wanted to use multiple parameters you would separate them using commas.

Another thing with declaring functions is you do NOT have to specify the return type, ie void or int. Because python will handle this for you.

Line 2: First thing you will notice is that this line is tabbed in. Think curly braces ( ' { ' and ' } ' ) when you see tabs. Its basically your tool for scoping.
the line 'print s' is inside of a function, thus it is tabbed in. You could use 3 tabs or 4 tabs or 18,927 tabs so long as you use the same number of tabs for everything in that scope. Most Python programmers just use one tab for every level of scoping. If I had an if statement inside of this function there would be another tab (total of 2) for everything in the if statement. If i had a loop inside the if statement I would use yet another tab (total of 3) for everything in the if statement. And so on and so forth.

Finally it says 'print s'. You'll notice we never declared s, but it was passed to the function when we called it. Thus the variable WAS declared but in a sneaky-sneaky way.

Line 3: We use the functions name to call the function followed by a set of parenthesis. We put a string inside of those parenthesis to pass to the function(ie variable s). If we had multiple parameters we would separate them using commas. If the function had no parameters we would just use an empty set of parentheses and it would look like: printSomething()


Moving on to getting input from the user, basic string manipulation, and an example of where you have to manually set the data type!

Code: Select all

input = str(raw_input("What is your name? "))
print "Hello" + input
print "Hello", input
print "Hello\n" + input
Lets break this code down line by line
Line 1: 'input' is the name of the variable i'm declaring. lets skip the str() call for now. 'raw_input' is the pythonic way of saying "Hey type something into the console for me bro". It takes a string as a parameter, which is essentially a print statement to prompt the user. Now the reason we need the str() call is because of the way Python handles variables. I know your thinking, "Wait Oh em gee, I thought python like, did all that variable junk for me???!?" Well for the most part it does. But if the user enters a number in for the name python will convert that into an int. NOT a string. so we have to force python to make the input a string no matter what the user enters. If we wanted to force the input to be a number we would use the int() call, however if the user entered letters in this case python would yell at you saying "Woh woh bro, that dude entered some letters and i wanted an integer.", so you have to be careful when forcing python to set variables to certain data types.

Line 2: You'll notice we use a + here, when python see's a '+' between to string it will automagically concatenate those 2 strings( Morph them together into one ub3r string)
Line 3: When we use a comma instead of a '+' It does almost the same thing except it appends a space to the first string before concatenating the strings together
Line 4: You'll notice the traditional line break escape sequence. "\n", This basically means print anything after this on a new line. If I used 3 "\n"s it would skip 3 lines before it printed something and so on and so forth. After the line break we just concatenated the 2 strings together and printed them to the screen!


Well, phew. Long wall of text! I hope this tutorial will help you transition from a C-style language background to a Pythonic one!

One more thing to mention is that Python is an object oriented programming language. So you can create objects, however thats a tutorial all in itself. :)
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Learning Python from a C-style language background

Post by hallsofvallhalla »

Awesome thanks for this!


You should work with my on my 3d MMO, it uses python and tons of it! I have been digging through Python for the past few days.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Learning Python from a C-style language background

Post by Jackolantern »

Very nice! :)

Even though I am pretty much at the throat of Python, it is getting more ubiquitous all the time. May as well get used to it... lol
The indelible lord of tl;dr
User avatar
Foofighter
Posts: 121
Joined: Sun Mar 04, 2012 1:52 pm

Re: Learning Python from a C-style language background

Post by Foofighter »

Hi,
thanks for this,
Iam pretty sure there is an older TUT on python i found it a few weeks ago but cant seem to find it now..

actually i also like python very much i worked myself into it the last 7weeks, i made an intensive Course on it where the overall goal was that u have made your own search engine at the end of the course. :) pretty amazing what u can learn just in this short time.
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Learning Python from a C-style language background

Post by mattykins »

Halls, I've seen the videos for your MMO and i'm not confident this computer could run it let alone help dev it! Lol, but if you need help with a script be sure to send me a message and i'll help as best I can.
Post Reply

Return to “Tutorials”