lets start then.
We'll begin with looking at an if statement:
If 1 == 1:
print('It's troo')
else:
print('I guess 1 isn't equal to 1')
Whats going on here is that first python checks if 1 = 1, if thats the case then it will print "It's troo". however if it isn't the case python will jump to the else clause and print "I guess 1 isn't equal to 1". Also python have no '({' clauses to mark the beginning or end of an if statement. Instead python uses indentation. Let's see an example of that.
While True:
If a == 1:
print('a is 1')
print('the things in this indentation')
print('is what will happen when a is 1')
Elif a == 2:
print('a is 2')
#this is checked after the first if statement
#meaning if a is 1 then this clause will be skipped
Else:
print('what is a anyway?')
#This clause will be skipped if either a is 1 or 2
print('this is outside of the else clause')
print('because its not in the else clause indentation')
print('This prints if the program manages to ')
print('break out of the infinite while loop')
So if your gonna write something that happens if an if statement is true then you write it in its own indentation and when you wanna break out of it you go down one indentation. What this does is making everyone write code in a similar way making it easier to read. If your still unsure about how this indentation works you can go play with some if statements and print in the command line.
As you can see in the previous example python uses boolean expressions to check if things are true or false. So how does a boolean expression look like in python.
#you can write if statements like this but i try to avoid it.
If a == 2: print('a is 2')
#If a is 2 then "a is 2" will be printed
#here follows some other boolean expressions
If a <= 2: #If a is less than or equal to 2.
If a >= 2: #If a is greater than or equal to 2.
If a < 2: #If a is less than 2.
If a > 2: #If a is greater than 2.
If a != 2: #If a is not equal to 2.
#you can also fill in several condition checks into one if statement
If a == 2 or a == 7: #If a is equal to 2 or if a is equal to 7.
If not a == 2: #If a isn't equal to 2.
If a < 7 and a > 2: #If a is less than 7 or greater than 2.
If any of this feels unclear you can do a Google search on boolean expressions or you could ask on codecall in the python section and I'm sure that ppl will come rushing to your aid.
But before we can put any of this to good use we need to know about variables. In python we have str, int, list, tuple and dictionary. Although going through them all and all their features would take an whole tutorial for them self. If you want to know more about the advanced features of different variables you can go to the site your gonna learn to love 5. Data Structures — Python v2.6.4 documentation . If you want to know anything about python it can most likely be found there.
Following are some easy operations to get you going on using variables.
#if you haven't noticed by know the '#' character is used to put in a comment #Strings str = 'hello' name = 'Andreas' # " or ' can be used to contain a string text = "This is my tutorial isn't it nice" #Watch out though because #Would end the string premature text = 'This is my tutorial isn't it nice' #if you want to write a str over several lines you can use this longtext = """This string can be written on several lines note that it has to be on the same indentation though""" #to add something to a str you use this part1 = 'hello' part2 = 'world' #this will make part1 = 'helloworld' part1 += part2 or part1 = part1 + part2 #to add a space you could insert one in this manner part1 = part1 + ' ' + part2 #which would make part1 = 'hello world' #to pick out a certain part of a str you can use this str = 'abcd' print(str[0]) #would output a print(str[2:3]) #would output c #you can play around with this some and #see what happens if you enter negative numbers #Integers a = 1 a += 2 #a = a + 2 print(a) #would output 3 #you can also assign several values in one line #even those of different types a, b, c, d = 1, 'hello', 2, 7 #or assign a single value to several variables a = b = c = 10
I'm gonna have to come back to lists, tuples, and dictionaries in another tutorial since they have lots of nice features. Feel free to browse around in the link provided earlier though to find some info about them.
Using this we can for example write a very simple (and insecure) login system.
name = 'Andreas'
password = 'abcd'
While True:
input_name = raw_input('enter your name: ')
input_password = raw_input('enter the password: ')
If input_name == name And input_password == password:
print('hello Andreas')
raw_input('press enter to continue')
Break
Else:
print('Did you misspell something?')
What will happen here is that when you enter "Andreas" as name and "abcd" as password it will greet you and wait for you to press enter. When you press enter it will break out of the while loop and the program will stop running. Watch out when your using break it might produce unwanted result when you nest while loops with either other while loops or for loops. So for example:
While True: For x in range(5): if x == 3: break
Would break out of the For loop, to fix this you can use this.
i = 1 While i == 1: For x in range(5): if x == 2: i = 0 Break
This would make i to 0 and break the for loop when x is 2. Which would in turn break the while loop seeing how i isn't 1 anymore.
A few words about for loops. A for loop is a loop that go through w.e you want to go through. Some examples:
#This will print out 0, 1, 2, 3, 4 For x in range(5): print(x) #this will print out a, b, c, d str = 'abcd' for x in str: print(x)
Basically a for loop will iterate through any iterable object.
It's funny when you start writing and it easy becomes more than you imagine it would. Well this should be enough to get you started doing some simple coding or get you interested enough to continue learning. I will try and go more in depth in the following tutorials.
Feel anything have been left out? Had trouble with something? Or want something changed?
Post a comment about it and I will see what I can do.


Sign In
Create Account



Back to top











