1.2+VariablesString+py

=1.2 Variable Types and Strings= As you may have seen, 1.1 may run into 1.2 and so there may be a bit of revision in this section but it needs to be done, none the less.

As you enter the wonderful world of python scripting, you will need to know the absolute basics of python, as said in the previous tutorial, the best way of learning is by doing, don't just run the tutorials but also try your own thing.

Variables again are containers you may like to think of as a cup, you can fill the cup with several different drinks and types of drinks, you can find out what is in the cup, you can change the contents and you can empty the cup. The general types of variables are: a string; such as “hello”, ‘rawr’,”foo” an integer; such as 1, 2, 3, 10, 1000 a floating point number; such as 1.6, 3.7, 4356.8689 a list; which is a list of objects signified by square braces, such as [1,4,7,3,5,7,9,3], [‘hello’,1,’rawr’] a dictionary; which is a list of keys with values signified by curly braces, such as{‘hello’:10,’blah’:’lol’,’foo’:30.6} a tuple; which is like an un-changeable list, signified by regular braces, such as (1,7,3,34,36.8),(“hello”,’blah’,1)

As you used in the last example, the syntax to create and fill a variable cup, is the variable name, then =, then the contents for example: code format="python" >>> string1 = "hello"                    ## assigns the string "hello" >>> integer1 = 20                        ## assigns the integer 20 >>> float1 = 1.9                         ## assigns the floating >>> list1 = [1,'hello','rawr',10.8]      ## assigns the list [1,'hello','rawr',10.8] >>> dict1 = {'hello':10,'rawr':'foo'}    ## assigns the dictionary {'hello':10,'rawr':'foo'} >>> tuple1 = (1,'hello',10.8)            ## assigns the tuple (1,'hello',10.8) code

Now to explain **The String**:

As shown before, a string is signified by either " " or ' ', and we can do some pretty cool things to these strings in python really easily! The first of the things we will do to a string, is called concatenation, this means to join two strings together, this can be done in two ways, either using a +(plus sign) or a ,(comma) for example: code format="python" >>>x = "hello" >>>y = "universe" >>>print x + y hellouniverse >>>print x, y hello universe code As you can see, they both concatenate differently, by using the '+', we directly concatenate two strings, leaving one big word, but by using ',' we concatenate with a space in the middle, seperating both words. We need to know both of these methods for different reasons, if we wanted to concatenate a string using +, but still wanted a space, we put it in our selves! so all we need to do is add a space to the end of x's string code format="python" >>> x = "hello " >>> y = "universe" >>>print x + y hello universe code Try some of your own examples, and remember to try and use raw_input in all of these examples! It will get you used to getting user input in your code and help u a lot in the long run!

Next we can talk about the *(multiplication) operator, this operator causes a string to be repeated a certain number of times via a continuous concatenation, for example: code format="python" >>>x = "hello" >>>print x + x + x + x hellohellohellohello >>>print x*4 hellohellohellohello code This operator makes it very easy to repeat strings and because we use an integer for how many repeats, we can use a special function 'int' on out raw_input function to get the user to tell us how many repeats! 'int' takes one argument of a string or float number and converts it (if possible) to an integer! Lets have an example of the 'int' function: code format="python" >>> word = "hello" >>> stringnum = '5'        ##set stringnum to the string '1', not the number >>> num = int(stringnum)   ##set b to the integer form of stringnum, which is 5 >>> print word*num hellohellohellohellohello code We'll talk more about integers in the next chapter, but you can now see a very important function for any python programmer! One VERY important way to play with a string is called slicing! This is a way to get or edit part of a string, by using square brackets at the end of the string/variable, for example: code format="python" >>> x = "hello universe" >>> print x[0:5]      ## print from the first character, up to but not including the 6th hello code As shown in this example, we use the square brackets to show the range of characters we want, the first number is where we want to start, and the second is where we want to end. you'll notice that we start at zero in python, as it is a zero base language like most. numbers in python will **always** start from zero! Using this form we can also change part of a string by making the slice of a string equal to another string! For example: code format="python" >>> x = "Hello Universe" >>> print x Hello Universe >>> x[0:5] = "Goodbye" >>> print x Goodbye Universe code In this example we create the string "Hello Universe", then we set the first 5 characters to the string "Goodbye", changing the "Hello" part of the string to "Goodbye"! Its very useful and a much faster and easier way than creating a new string!
 * 1) is like saying

Now we will continue on to the next part of the tutorial, Integers!

Prev Next