Python Concepts/Operators

Operations
There are certain things that you can do with all of the types. You can do simple math, index, slice, and check for membership. You can also use the Format Specifier in strings.

Simple Math
Math is very straightforward in Python. + adds, - subtracts, / divides, and believe it or not * multiplies. The main thing to comment on is %. % performs a division and then returns the remainder. This is called the modulus operation.

Type:

Now Type:

The % command can be used to find out if something is even or odd or if it is a multiple of some number. Here is some code that may be a little advanced for you right now. Just type it in and know that within a couple of lessons you will understand it fully. When you type  it must be indented greater than the last line. For example, we need to indent the code by at least one spaces or tab.

Like C++ or Java, Python knows 2 different types of "Equality" ("="): Equality as a command or assignment of a value ( meaning make variable   equal 2); and Equality in conditions or tests ("if a == 2 ..." meaning that variable a is checked and not assigned). In the first case, use the single equal-sign "="; in the second case use the double equal-sign "==".

Type:

You can also use variables, and elements and values in simple arithmetic.

Type:

That is how the variable works.

Type:

This is how you use elements from a list to preform arithmetic operations. It should be clarified that x[0] = 1, x[1] = 2, and x[2] = 3.

You can also add and multiply strings, tuples, and lists. Type:

That is how you do arithmetic with values from a dictionary. Don't forget to use quotations around the keys unless you use integers as the keys. Spend a couple of minutes messing around with this stuff, its fun and it'll help you remember it better. You may also add strings, tuples, and lists.

Concatenating
To add two strings together - to do this you just type the first string, an addition sign, the second string.

Type:

You can do the same with variables referring to strings. Type:

You can do the same with tuples. Type:

It works the same with lists. What you cannot do is combine two different kinds of types. Type:

Multiplying types
Multiplying is very easy and straight forward. Type:

Now with a tuple. Type:

You cannot do the same with dictionaries; that would make multiple keys with the same entry name, which isn't valid in Python.

Type:

Indexing
You have already learned how to index in lesson 2. When you typed x[3] you were indexing. You may also index a string without first making a variable represent it.

Type:

simple enough.

Slicing
Slicing is used to access a range of elements the way that indexing accesses one element.

Type:

The numbers that you enter after the variable (the [14:32]) are called indices. The way the last one works is hard to understand at first. I'll do another example to make it clear.

Type: It prints out the 2 because it starts numbering at 0, but the reason it prints out the 3 instead of the 4 is because it counts the  that you entered as the end point and does not include it. Seems weird at first but easy to get used to.

You may also use negative numbers as the indices. If you only use one negative index  Python will print starting with the element selected and go until the end of the string, tuple or list. This works for positive indices as well. Python will print the first 3 elements of.

Another thing that you can do when splicing is enter the step index. Type:

You may also just use the step index. Type:

You can do this to find every other element or every third or whatever your needs require. You may also make the step index negative. By doing so you make the elements return starting with the last one.

Type:

To reverse a string (or any sequence):

When the step index is negative it is important to remember to make sure that the first index is calling an element that comes later in the string, tuple or list than the second element. Just as in the last example  is representing   while   is representing.

The Format Specifier
You are going to use this versatile command constantly. Type: What the percent sign does is hold the place in the string and let you tell Python what to put there at the end of the string. The s behind the % tells Python to expect any input. If you neglect the s, Python will automatically assume it to be %d, which causes Python to expect a numerical value. Be sure to define it correctly, or the script won't work.