Python - The Absolute Basics 04-14-2014, 01:27 PM
#1
Python - The Absolute Basics
- // Installation //
- // Modules //
- // Hello, World //
- // Variables //
- // Strings //
- // Integers //
- // Math //
- // If / Else statements //
Created by Duubz, for you. <3
Installation
Python is made from C, a pretty generic and very useful language. We've all probably heard of it. However, Python isn't built into your computer, unless you're using a custom OS, so you need to download it. Python can be downloaded from the official Python website, https://www.python.org/. The most stable versions of Python would be Python 2.7.6 and Python 3.4.0. There are lots of ways to go when getting your version of Python, best to go with the most recent, or most stable. The syntax of Python changes from Python2 to Python3, so depending on which version you get, you may want to work differently. Also, keep in mind Python has many differences from the Windows and Linux version. Of course, Mac does too, but it doesn't have as big of an impact as Windows and Linux does.
Once you've downloaded the installer for Python, don't just click next. Depending on how you install Python, you may or may not be able to run it in the terminal/command prompt, which doesn't seem like much, but can make a big difference on how you use Python. First, you'll probably want to select which users can or cannot use Python. More often than not, it's best that all users have access to Python. However, for you control freaks out there, you can limit it to just your user profile on your computer. Another setting you will be prompted on while installing Python is if it can be used through the command prompt/terminal. This impacts how you use Python a lot, because if you do use it, you have to worry about things such as clearing the window, which is big because all three main operating systems use a different method to this.
Once your settings and such have been set, you just need to wait on Python to install. This doesn't take much time at all, you can probably just sit still for about a minute and you'll have finished your installation.
Modules
Modules will probably be one of the biggest dependencies when making programs with Python. Without modules, you will very rarely have a working program unless you know exactly what you're doing. Modules work kind of like a tree. You can make a folder, name it whatever you want, then put python files inside of it. Or put more folders inside. For every file that is inside of that module, you will need to put a period/dot to split these when importing them.
Importing modules makes the difference between a working program, and one that can't even work past the first line. When you first open up the Python GUI and import a module, the word import will turn yellow. This is because of the Python syntax. For every module you import, you can split them by commas. i.e. import module1,module2.
An example of importing different modules from the same tree is urllib.request.
Just from that one line, we can see that urllib is a folder, and request is a module. Don't believe me?
![[Image: 12ti6xq.png]](http://i.imgur.com/12ti6xq.png)
![[Image: zLWLxo0.png]](http://i.imgur.com/zLWLxo0.png)
So, if we made a folder that had a folder in a folder with a python file in it, to import this module we would put
Hello, World
A Hello, World program is probably the most simple program that one can make. In Python, we will use the print function. This will put whatever we put into it on screen. Since Python focus on readability, this program is made really short and simple. You don't need modules either.
Now because the syntax changed a little in-between version 2 and 3, we can apply a little "spice" to this program. We can have it detect our python version, then print it to our syntax.
With that, we can see how much modules will affect our programs. That only shows very minimum though, because you can easily just put different syntax because you know[/i your Python version. This doesn't make much sense at the moment, but if you stop and think that you will be allowing programs that you made to the general public, their versions may differ, thus it's always good import a module in this case. However, syntax isn't the only thing that changes between versions. So do modules. Many modules have differentiated between version 2 and 3, so if you do release a program that you've made, be sure to specify which version it was made for. And, probably best to make multiple versions of that program so it's compatible with more than just one version of Python.
Variables
Variables are very important to anything you program. Some variables are lists, some are booleans, statements, etc., but no matter the variable type, you will always need them when programming. Setting variables in one language is different from the other. An example of this would be PHP, JavaScript, and even Python itself. Python has a very simple way of setting variables, no need to put special characters or of that sort. Here's an example of setting variables in PHP, JavaScript, and Python.
(PHP & JavaScript)
(Python)
Like in most languages, variables set in Python are case sensitive. If I set variable VaRiAbLe, I have to also type out VaRiAbLe to reference it in my code. Otherwise, I would not be using VaRiAbLe, and you would get an error unless you set VARiabLE or something of the sort. This also means I can set Variable, and vARIABLE to be two different things. It's kind of like a double-edged sword, useful and at the same time can be useless in some cases.
Strings
Strings are used when doing arithmetic, printing statements, and of the sort. Variables are strings too. However, to specify the use of a string that is not in a class, or even definition, we must set a global variable. This can be done by typing out "global" followed by our variable. Then we can use it in our class or definition if it isn't already in there. This helps us a lot because we don't need to type out our variables over and over again.
An example of the use of strings would be as follows.
Our variable name is used as a string, then printed. However, if we use an integer as a string, we might have an error.
We will cover integers later, but these can also be used as strings. We need to use "str", short for string, to call our "int", short for integer, in our code.
When calling strings, we can use a few different ways to concatenate them to our statement, arithmetic, or whatever.
Using the percent operator:
We can put "%s" in our statement then call it later. However, they must be called the same order they are placed in the statement.
i.e.
Using .format:
Put curly brackettes with a number inside, starting from zero, ascending. Can be in any order in the statement, but must be in the order they are called in the actual format.
i.e.
Using plus signs:
Use plus sign to add strings to your statements. Always have to stop the statement and begin it again when using this, can be quite annoying.
i.e.
Integers
Integers are basically just numbers with no decimal. Numbers with decimals would be called floats, and we won't be going over those in this thread. Mainly because it's 6 in the morning, an hour before school, and I've already spent over an hour writing this thing.
You can set variables as integers. Using "int", you can check if a value is an integer. An example of this is doing int(8). Eight is an integer, but if we did int(8.1), we would get an error. The "sister" to integers would be the floats, or numbers with decimals. And they can all be used in arithmetic, which will be in the next section.
Math
Math, ah yes, that class we all hate. Math can also be done with Python. All you have to do is write out an expression. It's not exactly the same as in math class though. Here's a chart of each of the four basic expressions done in Python.
Math is also an importable module, but you can do basic arithmatic without the module itself. However, if we wanted to get the value of pi, or something of the sort, we'd need to import math.
If/Else Statements
If and else statements are pretty mandatory in a programming language. If you don't have these, how are you making any [i]useful programs? Like, wot? In any case, yes, Python has these statements.
You can set variables to be true or false to. Because of Python's syntax, you must always capitalize them. No, not throw random capitals around, for you teenage girls out there, you only capitalize the first letter.
With if/else statements, you can use true and false statements as switches. If x and y is true, then we will add one to z. Otherwise, we will subtract one from z.
With that code, z will equal 6. However, if we set x, y, or both x and y to False, then the z will equal 4. Or 6, if you did this in Python GUI.
That's about it. If you have any questions, comments, or just need help in general, message me, or drop a reply below. Keep in mind this is only the bare, naked, basics of Python programming you can do MUCH more.
~ Duubz
- // Installation //
- // Modules //
- // Hello, World //
- // Variables //
- // Strings //
- // Integers //
- // Math //
- // If / Else statements //
Created by Duubz, for you. <3
Installation
Python is made from C, a pretty generic and very useful language. We've all probably heard of it. However, Python isn't built into your computer, unless you're using a custom OS, so you need to download it. Python can be downloaded from the official Python website, https://www.python.org/. The most stable versions of Python would be Python 2.7.6 and Python 3.4.0. There are lots of ways to go when getting your version of Python, best to go with the most recent, or most stable. The syntax of Python changes from Python2 to Python3, so depending on which version you get, you may want to work differently. Also, keep in mind Python has many differences from the Windows and Linux version. Of course, Mac does too, but it doesn't have as big of an impact as Windows and Linux does.
Once you've downloaded the installer for Python, don't just click next. Depending on how you install Python, you may or may not be able to run it in the terminal/command prompt, which doesn't seem like much, but can make a big difference on how you use Python. First, you'll probably want to select which users can or cannot use Python. More often than not, it's best that all users have access to Python. However, for you control freaks out there, you can limit it to just your user profile on your computer. Another setting you will be prompted on while installing Python is if it can be used through the command prompt/terminal. This impacts how you use Python a lot, because if you do use it, you have to worry about things such as clearing the window, which is big because all three main operating systems use a different method to this.
Once your settings and such have been set, you just need to wait on Python to install. This doesn't take much time at all, you can probably just sit still for about a minute and you'll have finished your installation.
Modules
Modules will probably be one of the biggest dependencies when making programs with Python. Without modules, you will very rarely have a working program unless you know exactly what you're doing. Modules work kind of like a tree. You can make a folder, name it whatever you want, then put python files inside of it. Or put more folders inside. For every file that is inside of that module, you will need to put a period/dot to split these when importing them.
Importing modules makes the difference between a working program, and one that can't even work past the first line. When you first open up the Python GUI and import a module, the word import will turn yellow. This is because of the Python syntax. For every module you import, you can split them by commas. i.e. import module1,module2.
An example of importing different modules from the same tree is urllib.request.
Code:
import urllib.requestJust from that one line, we can see that urllib is a folder, and request is a module. Don't believe me?
![[Image: 12ti6xq.png]](http://i.imgur.com/12ti6xq.png)
![[Image: zLWLxo0.png]](http://i.imgur.com/zLWLxo0.png)
So, if we made a folder that had a folder in a folder with a python file in it, to import this module we would put
Code:
import folder.folder.folder.moduleHello, World
A Hello, World program is probably the most simple program that one can make. In Python, we will use the print function. This will put whatever we put into it on screen. Since Python focus on readability, this program is made really short and simple. You don't need modules either.
Code:
print("Hello, World!")Now because the syntax changed a little in-between version 2 and 3, we can apply a little "spice" to this program. We can have it detect our python version, then print it to our syntax.
Code:
import sys
if sys.version_info[0] > 2: print("Hello, World!")
else: print "Hello, World!"With that, we can see how much modules will affect our programs. That only shows very minimum though, because you can easily just put different syntax because you know[/i your Python version. This doesn't make much sense at the moment, but if you stop and think that you will be allowing programs that you made to the general public, their versions may differ, thus it's always good import a module in this case. However, syntax isn't the only thing that changes between versions. So do modules. Many modules have differentiated between version 2 and 3, so if you do release a program that you've made, be sure to specify which version it was made for. And, probably best to make multiple versions of that program so it's compatible with more than just one version of Python.
Variables
Variables are very important to anything you program. Some variables are lists, some are booleans, statements, etc., but no matter the variable type, you will always need them when programming. Setting variables in one language is different from the other. An example of this would be PHP, JavaScript, and even Python itself. Python has a very simple way of setting variables, no need to put special characters or of that sort. Here's an example of setting variables in PHP, JavaScript, and Python.
(PHP & JavaScript)
PHP Code:
$name = "Duubz";
Code:
var name = "Duubz";(Python)
Code:
name = "Duubz"Like in most languages, variables set in Python are case sensitive. If I set variable VaRiAbLe, I have to also type out VaRiAbLe to reference it in my code. Otherwise, I would not be using VaRiAbLe, and you would get an error unless you set VARiabLE or something of the sort. This also means I can set Variable, and vARIABLE to be two different things. It's kind of like a double-edged sword, useful and at the same time can be useless in some cases.
Strings
Strings are used when doing arithmetic, printing statements, and of the sort. Variables are strings too. However, to specify the use of a string that is not in a class, or even definition, we must set a global variable. This can be done by typing out "global" followed by our variable. Then we can use it in our class or definition if it isn't already in there. This helps us a lot because we don't need to type out our variables over and over again.
An example of the use of strings would be as follows.
Code:
name = "Duubz"
print(name)Our variable name is used as a string, then printed. However, if we use an integer as a string, we might have an error.
Code:
number = 2
print("You picked number "+number+"!")We will cover integers later, but these can also be used as strings. We need to use "str", short for string, to call our "int", short for integer, in our code.
Code:
number = 2
print("You picked number "+str(int(number))+"!")When calling strings, we can use a few different ways to concatenate them to our statement, arithmetic, or whatever.
- Percent operators - Use percent operator to concatenate strings in a certain order.
- Format - Use .format to concatenate our strings in a certain order, without stopping our statement.
- Plus - Using the plus sign to directly add our strings. Using this can put bullet holes in your code.
Using the percent operator:
We can put "%s" in our statement then call it later. However, they must be called the same order they are placed in the statement.
i.e.
Code:
print('Hello, my %s is %s.' % ('name','Duubz'))Using .format:
Put curly brackettes with a number inside, starting from zero, ascending. Can be in any order in the statement, but must be in the order they are called in the actual format.
i.e.
Code:
print('Hello, my {0} is {1}'.format('name','Duubz'))Using plus signs:
Use plus sign to add strings to your statements. Always have to stop the statement and begin it again when using this, can be quite annoying.
i.e.
Code:
print('Hello, my'+'name'+'is'+'Duubz')Integers
Integers are basically just numbers with no decimal. Numbers with decimals would be called floats, and we won't be going over those in this thread. Mainly because it's 6 in the morning, an hour before school, and I've already spent over an hour writing this thing.
You can set variables as integers. Using "int", you can check if a value is an integer. An example of this is doing int(8). Eight is an integer, but if we did int(8.1), we would get an error. The "sister" to integers would be the floats, or numbers with decimals. And they can all be used in arithmetic, which will be in the next section.
Math
Math, ah yes, that class we all hate. Math can also be done with Python. All you have to do is write out an expression. It's not exactly the same as in math class though. Here's a chart of each of the four basic expressions done in Python.
Code:
print(5 + 5) # ADDITION
print(5 - 5) # SUBTRACTION
print(5 * 5) # MULTIPLICATION
print(5 / 5) # DIVISIONMath is also an importable module, but you can do basic arithmatic without the module itself. However, if we wanted to get the value of pi, or something of the sort, we'd need to import math.
If/Else Statements
If and else statements are pretty mandatory in a programming language. If you don't have these, how are you making any [i]useful programs? Like, wot? In any case, yes, Python has these statements.
You can set variables to be true or false to. Because of Python's syntax, you must always capitalize them. No, not throw random capitals around, for you teenage girls out there, you only capitalize the first letter.
With if/else statements, you can use true and false statements as switches. If x and y is true, then we will add one to z. Otherwise, we will subtract one from z.
Code:
z = 5
x,y = True,True
if (x == True) and (y == True): z += 1
else: z -= 1
print(z)With that code, z will equal 6. However, if we set x, y, or both x and y to False, then the z will equal 4. Or 6, if you did this in Python GUI.
That's about it. If you have any questions, comments, or just need help in general, message me, or drop a reply below. Keep in mind this is only the bare, naked, basics of Python programming you can do MUCH more.
~ Duubz
![[Image: BXqGARG.png]](https://i.imgur.com/BXqGARG.png)









![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: screenshot_108.png]](http://s21.postimg.org/reblu4von/screenshot_108.png)


