Introduction to Python Programming - #2 First Program 01-21-2015, 04:16 PM
#1
Now that you have the tools, let's jump into our first program.
Create and open a new file. Name it anything.
In Python3, the biggest change is that 'print' is not longer a statement, and is now a function: 'print()'.
That's also an example of the simplest program known to man, printing 'Hello, World!!!'. In python, to 'print' something, is to display it on the screen. Typing the following onto the file will print 'I like Oni's Dong.' onto the screen. Try it.
print() is a function. A function is something that takes one or more inputs, and gives you an output. Functions can either be coded by you or imported and then called.
Functions:
The first line defines the function 'gimmeMore()' which takes one argument: 'in', which will be set as a local variable. So, let's say that the function is called like so:
What the function will run is:
After that, all the indented code is part of the function. The second line prints 'in', whatever it is, 31337 times. But, if you just typed that in, nothing would happen. You'd need to call that function:
You'll also notice the comments. Namely the lines preceeded by '#'. This symbol indicates that everything after it on the same line should be ignored.
Importing and running a function is similar. Let's import the 'time' module.
Line one imports the time module, so that the functions within it can be used. The function that we do use is the sleep() one. To call it, we did time.sleep() instead of just sleep(), so it called the function sleep() from the time module. Have a go yourself.
Stay tuned for the next thread, where I'll go over some datatypes.
Create and open a new file. Name it anything.
Code:
nano anything.pyIn Python3, the biggest change is that 'print' is not longer a statement, and is now a function: 'print()'.
Code:
Python2: print "Hello, World!!!\n"
Python3: print("Hello, World!!!\n")That's also an example of the simplest program known to man, printing 'Hello, World!!!'. In python, to 'print' something, is to display it on the screen. Typing the following onto the file will print 'I like Oni's Dong.' onto the screen. Try it.
Code:
print("I like Oni's Dong.")print() is a function. A function is something that takes one or more inputs, and gives you an output. Functions can either be coded by you or imported and then called.
Functions:
Code:
def gimmeMore(in): #Declare function
print(in * 31337) #Execute indented codeThe first line defines the function 'gimmeMore()' which takes one argument: 'in', which will be set as a local variable. So, let's say that the function is called like so:
Code:
gimmeMore("Hello")What the function will run is:
Code:
print("Hello" * 31337)After that, all the indented code is part of the function. The second line prints 'in', whatever it is, 31337 times. But, if you just typed that in, nothing would happen. You'd need to call that function:
Code:
gimmeMore("I like Oni's Dongs. ")You'll also notice the comments. Namely the lines preceeded by '#'. This symbol indicates that everything after it on the same line should be ignored.
Importing and running a function is similar. Let's import the 'time' module.
Code:
import time
print("Hello")
time.sleep(1)
print("lel")Line one imports the time module, so that the functions within it can be used. The function that we do use is the sleep() one. To call it, we did time.sleep() instead of just sleep(), so it called the function sleep() from the time module. Have a go yourself.
Stay tuned for the next thread, where I'll go over some datatypes.
















![[+]](https://sinister.li/images/modern/collapse_collapsed.png)