Content...
Introduction to python1.Installation
To start coding in python, you will need the interpreter to begin with. I suggest the latest version, which seems to be the best choice for all.
Python 2.5 Windows (32Bit)
Python 2.5 Windows (64Bit)
Python 2.5 MacM
If you are using linux (mostly any distro I believe works) run this in a terminal window:
sudo apt-get install python
This will install the latest version of python on your computer. You most likely will not need to install python on mac or linux as python comes pre-installed for these.
After you have obtained the installer, simply run the file (you may need root/administrator privileges).
2.1Basic words to know
| Integer | A number. 31, 4, 1942122 are all examples of integers. |
| String | A block of text. "Words are cool", "text", "what" are all strings. |
| Function/Definition | A block of code that can be referred to for later use/multiple uses. |
| Concatenation | The combining of integers, strings, or variables all in one variable or string for output. |
| Module | A group of functions put together in one file which can be "imported" and it's functions can be used. |
2.2Basic Functions
Commenting. You may not see it's importance now, but when you advance later on and change styles of coding, comments can help you remember why you did certain things, explain things that you may have forgotten, or simply be used as a reminder to tell you to fix something.
Commenting is very easy to understand, and can be used anywhere throughout your code.
# This is a comment!
Aside from commenting, there is something very basic, yet very useful. These are called variables. You can modify variables, or use them to save time for you, etc. Their uses are endless.
#below, we will set the variable name with the value "value".
name = "value"
name = "value"
Now, what's the point of setting things if you can't output anything? This is where the print function comes in.
# Lets print some text!
print "some text!"
Variables are pointless without any use, right? Lets combine what we learned about variables and the print function together... Notice, variable names do NOT have any form of quotes around them.print "some text!"
# Set the variable first...
variable = "test"
# This will output the variable in command line
print variable # This will print "test".
variable = "test"
# This will output the variable in command line
print variable # This will print "test".
(Note: If you are on windows, and you run the file directly from a folder or your desktop, and a "black box" appears then dissappears, you need to use it from the command line. Open up command prompt, type in this "cd C:\Directory\where\your\python\script\is" of course, edit the path accordingly. Then type in "yourscript.py" or "python yourscript.py" if the first does not work.) This concludes section 2.2 .
2.3Concatenation
When you have a variable, you don't just output it without any context. So this is where we use concatenation. With concatenation, we can use variables in a sentences, as well as integers.
# Lets set the variable...
variable = "testing"
# Lets concatenate!
print "We are "+variable+"!"
variable = "testing"
# Lets concatenate!
print "We are "+variable+"!"
The plus (+) is for concatenation. The variable's value is "testing", and we concatenated the variable in between our string. This will now output "We are testing!".
Concatenation can also be done with integers and strings. (Note: integers do not have quotes around them; if they do, they will be treated as strings)
# Set our variable as an integer
ourinteger = 13
# Lets output the integer in a string!
print "This is our integer: "+ourinteger+"!"
As you may have noticed while running the script, the only output will be an error message, saying "Cannot concatenate str and int" or something very similar. To do that, we must treat the variable as a string, but not modifiy it in any way.ourinteger = 13
# Lets output the integer in a string!
print "This is our integer: "+ourinteger+"!"
# Set our variable as an integer
ourinteger = 13
# Lets output the integer in a string! WITHOUT ERRORS TOO!
print "This is our integer: "+str(ourinteger)+"!"
ourinteger = 13
# Lets output the integer in a string! WITHOUT ERRORS TOO!
print "This is our integer: "+str(ourinteger)+"!"
If you haven't noticed, str is short for string, as int is short for integer. If for any reason you have to treat a string as an integer, you can simply use the "int()" function.
We can also concatenate while setting variables:
stringone = "Part 1"
stringtwo = "Part 2"
complete = stringone +" and "+stringtwo
print complete # Will out put "Part 1 and Part 2"
stringtwo = "Part 2"
complete = stringone +" and "+stringtwo
print complete # Will out put "Part 1 and Part 2"
Quotes are very important, and can give you errors if used improperly. If we wanted to use the phrase "I don"t like cookies", we would get an error. We need to do something known as "escaping" quotes. Escaping quotes need to be done if you use the same kind of quotes in a string (unless it's for concatenation).
# Non-escaped string, will give an error
print "He said "yes" to my question"
# Will not give an error, because we escaped the quotes that are part of the string and not concatenation
print "He said \"yes\" to my question"
print "He said "yes" to my question"
# Will not give an error, because we escaped the quotes that are part of the string and not concatenation
print "He said \"yes\" to my question"
3.1String Manipulation
Some time or another, we will need to modify strings. You will find a list of ways to modify strings to your liking.
# Set variable first...
ourstring = "this is my STRING"
# Turn a string to all capitals...
print ourstring.upper()
# Turn a string to all lower case...
print ourstring.lower()
# Swap the case for a string (Upper becomes lower, lower becomes upper)
print ourstring.swapcase()
# Capitalize the first character and turn the rest to lower case...
print ourstring.capitalize()
# Get rid of all whitespace such as tabs and spaces from the end and beginning of a string
print ourstring.strip()
# Capitalize the first letter of every word, and turn the rest into lower case...
print ourstring.title()
ourstring = "this is my STRING"
# Turn a string to all capitals...
print ourstring.upper()
# Turn a string to all lower case...
print ourstring.lower()
# Swap the case for a string (Upper becomes lower, lower becomes upper)
print ourstring.swapcase()
# Capitalize the first character and turn the rest to lower case...
print ourstring.capitalize()
# Get rid of all whitespace such as tabs and spaces from the end and beginning of a string
print ourstring.strip()
# Capitalize the first letter of every word, and turn the rest into lower case...
print ourstring.title()
Its suggested that you actually try this out with several different strings to get the feel on how things work.
3.2Lists
Next up is lists. They're a bunch of values stored in one variable. Below is an example of a list:
list = ["apples", "oranges", "bananas"]
Lists can hold items all at once, as shown above, or each item can be added separately, as shown below:
list[0] = "apples"
list[1] = "oranges"
list[2] = "bananas"
list[1] = "oranges"
list[2] = "bananas"
All lists begin with "0" unless otherwise specified. Now, lets try getting the variables out of a list.
list = ["apples", "oranges", "bananas"]
print "I like fruits such as "+list[0]+", "+list[1]+", and "+list[2]+"."
print "I like fruits such as "+list[0]+", "+list[1]+", and "+list[2]+"."
What if you already made the list, but wanted to add an item to the end? Or what if you wanted to know how many items were in a list?
# Lets add cookies to the list!
list = ["apples", "oranges", "bananas"]
list.append("cookies")
# Lets see how many items are in the list...
print len(list)
list = ["apples", "oranges", "bananas"]
list.append("cookies")
# Lets see how many items are in the list...
print len(list)
(Note: len() can be used for strings as well)
