Basics Instructions

Printing text:

>>> print("Hello Oscar")
Hello Oscar

Comments:

>>> # This is a comment

Arithmetic operators and Extra functions:

>>> 1+2*(5-2)/4 # The result is an iteger
2
>>> 1+2*(5-2)/4.0 # The result is a real cuz one of them is a real (4.0)
2.5

Assignment operator: Works similar to other languages​​.​​

Assigning an integer value to a.

>>> a = 1*2*3*4*5
>>> print(a) # Printing the variable: a

Assigning a vector (unidimensional array) to a, this is well know also as LIST.

>>> a = [1, 2, 3]

Assigning a string (in fact a string of chars) to a.

>>> a = "Oscar Giraldo"

More Functions:

>>> max(2.5,2) 
2.5
>>> min(-5,5)
-5
>>> max(2.5,2) 
2.5
>>> min(-5,5)
-5
>>> 77%10 # modulo operator: Calculate the excess residue division.
7
>>> 10%77
10

Sorting a List.

>>> sorted([4, 3, 1, 4, 5])
[1, 2, 3, 4, 5]

Or:

>>> a = [4, 3, 1, 4, 5]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]

Extra Functions: Here

Some operations among Lists(vectors):

Accessing vector elements.

>>> a = "Oscar Giraldo" 
>>> a[0] # Each character occupies a position starting from 0
'O'
>>> a[1]
's'
>>> a[-1] # And we can also go backward 
'o'
>>> a[-2]
'd'
>>> a = "Oscar Giraldo" 
>>> a[2:5] # a[start:stop]
'car'
>>> a[6:] 
'Giraldo'

Replicating a list

>>> a = [1, 2, 3]
>>> a + a + a 
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> 4*a
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

It's time to go kind of further :)

Unlike strings, which are immutable, …

>>> a = 'oscar'
>>> a[0] = 'p'
 
Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    a[0] = 'p'
TypeError: 'str' object does not support item assignment

… it is possible to change individual elements of a list.

A list is a great concept where you can append into it a mix of different extructures, such as strings, integers or even more lists. Take for example this singular list:

>>> a=['drum', 'guitar', 'bass', 'voice', [1, 2, 3], 3.1415]

So we access the list in the following way:

>>> a[0][3] + a[1][1] + a[2][2] + a[3][2] + a[3][3] # The 1st index refers to the one of the elements on the list, and the 2nd pick a component of this element  
'music' # Linking all the partial results we get the word 'music'

Or we can change a value of it:

>>> a[1] = "I play the " + a[1]
'I play the guitar'
>>> print(a) # The whole list has already changed
['drum', 'I play the guitar', 'bass', 'keyboard', 'voice', [1, 2, 3], 3.1415]

And if we want to append an element:

>>> a + [2,4]
['drum', 'I play the guitar', 'bass', 'keyboard', 'voice', [1, 2, 3], 3.1415, 2, 4]] # The integers 2 and 4 were appended

Or a list:

>>> a+[[2, 7]]
['drum', 'I play the guitar', 'bass', 'keyboard', 'voice', [1, 2, 3], 3.1415, 2, 4, [2, 7]]

Or if we want to remove some one of them:

>>> a
['drum', 'I play the guitar', 'bass', 'keyboard', 'voice', [1, 2, 3], 3.1415, 2, 4, [2, 7]]
>>> a[5:10] = []
>>> a
['drum', 'I play the guitar', 'bass', 'keyboard', 'voice']

Some useful functions:

  • len
>>> a = 'have fun'
>>> len(a)
8
  • append
>>> a = ['we','r']
>>> a.append('young')
>>> a
['we', 'r', 'young']
  • insert
>>> a = [1, 3]
>>> a.insert(1,2) # a.insert(desire target position, desire element)
>>> a
[1, 2, 3]
  • remove
>>> a = ['we', 'r', 'bored']
>>> a.remove('r')
>>> a
['we', 'bored']
>>> a.insert(1,'are NOT')
>>> a
['we', 'are NOT', 'bored']
  • pop
>>> a = [1, 10, 2, 3, 4, 5, 20]
>>> a.pop(1) # return the indexed element and remove it from the list
10
>>> a.pop() # by default apply for the last element
20
  • index
>>> a = [1, 2, 3, 4, 1, 5]
>>> a.index(1) # a.index(x) returns the index in the list of the first item whose value is x. In our case x is 1
0
>>> a.index(11) # be sure that the element exist
 
Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    a.index(11)
ValueError: 11 is not in list
  • count
>>> a = [1, 2, 3, 4, 1, 5]
>>> a.count(1) # a.count(x) returns the number of times x appears in the list.
2
>>> a = 'zepellin'
>>> a.count('e')
2
  • reverse
>>> a=['u', 'n', 'i']
>>> a.reverse() # Reverse the elements of the list, in place.
>>> a
['i', 'n', 'u']

Comments

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License