Re: Python crash course

I do recommend python 3.x, however that's just my viewpoint. Most code is easily portable to python3 using lib2to3 but some of it does not port properly and requires editing to work properly. The tutorial I'm about to give, which expands on cae_jones's tutorial, is for Python 3 programmers, although some of it will work on python 2 as well.

Functions, conditionals, and loops

If you did not understand the code writtein in post 3, I will try and explain, in some detail, most of the elements used in that post to clerify exactly what is done. I will, however, not relate to such elements.

Functions

A function is a way of categorizing a lot of code into a simple one liner. For instance, a call to a function called play_3d_sound does a lot of things using that one function call. The function is defined using the def keyword. The code is indented; tabs or spaces are accepted, but a mix of both will result in a syntax error.
The s yntax for defining a function is as follows:

def <function name> ():
# code

or

def <function name> (<argument 1>, <argument 2>, ...):
# code

If you look at each portion of the function definition syntax, you will notice that the items after the colon (":") are indented by one space. This is called the "indent level" and must always remain the same through the code while writing the function. The only time this should ever differ is if your inside a loop, conditional, class, try-accept block, or with block, or if you have finished the function. We will cover with blocks and try-accept blocks later.
An example function would be as follows:

def hello (name):
print ("Hello,", str(name))

Let's brea k this function down into segments:

def hello (name):

This creates the function. A function cannot be called unless it has been defined beforehand.

print ("hello", str(name))

This is the code of the function. Think of a function as a document on your computer. Inside the function is code that the function utilizes, so think of that code as text you write. The function is your document, which was blank upon definition of the function, but when you add code, your writing paragraphs for, say, a book or thesis paper.
Did you notice the str() call inside of the print() statement? It is possible to call functions inside of other functions, just like you can open more doors inside of a building.
Keep in mind that if you write this function and never call it, it will sit in memory, never being used. So, let's rewrite the funct ion, this time calling it:

def hello (name):
print ("Hello,", str(name))

hello("Ethin")

Output:

>>> hello("Ethin")
Hello, Ethin!
>>>

Did you notice the blank line after the function definition? This is the terminator of the function. This is not required; Pythonists just use it as an indicator to them that says "Hey, this function ends here." This is also used for terminating loops, classes, exception catchers, etc. You will see it quite often.

Conditionals

If you paid attention in geometry class, you will no that a conditional says "if so and so, then so and so." Of course, you will know that a conditional can be conversed and inversed: "if A, then B.", "if B, then A.", and "If not A, then B." or "If A is not B, then C."
Python has its own way of doing this:

if A:
# code

if not A:
# code

Of course, there are if... else... elif... conditionals, which go like this:

if A:
# code
elif B:
# code
else:
#code

An example conditional would be as follows:

i = 15
if i * i = 225:
print ("i is 225!")
elif i * i * i = 3375:
print ("i is 3375!")
else:
print ("i is not 225 or 3375!")

As you noticed, we defined a variable. A variable must be defined before we could use it.

Loops

A loop is a block which basically says, "If A is B, then do B until C."
There is also a loop which says, "While A is B, then C" and "while A is in B, then C."
These loops are called the for and while loops. The for syntax is as follows:

for a in b:
# code

Replace 'a' with a variable, and replace 'b' with a list, dictionary, or function call. For instance, here's the most common for loop I use:

for i in range (1, 101):
print (i)

or

for i in xrange (1, 101, 5):
print (i)

A while loop looks similar:

i = 0
while i < 100:
print (i)
i = i + 1

This does the following:

  1. Declare the variable i with the value 0.

  2. Enter the while loop, which conditionally says, "while i is less than 100: <code>"

  3. Print i's value.

  4. Increase i by 1, while keeping the current value.

If we had simply done:

i = i

We would have caused an infinite loop, which would have run, indefinitely until control+c (CIGINT (signal interrupt)) was pressed.

Well, folks, that's all for now. See you later.

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : momo7807 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dhruv via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dhruv via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : frastlin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dhruv via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector

Reply via email to