Fundamentals (Python)

From Linewaves
Jump to navigation Jump to search

Python is a general-purpose programming language widely used in a variety of fields, including computational humanities research (and computational musicology in particular).

Once you have Python installed, type the following and hit enter to print your first message:

print("Hello world!")

To get:

Hello world!

The print function displays the elements in parentheses in the console window. This can be a string (as above, in quotation marks) or a variable that has been previously defined:

a = "Hello world!"

print(a)

We use the = symbol to assign a value to a variable in Python. In other programming languages such as C and JavaScript, variables must be declared before they can be assigned a value. This is not necessary in Python.

As with other programming languages, Python recognizes a number of different data types. Some of the most common types are strings, integers, floating-point numbers, and lists:

my_string = "Hello World!"

my_integer = 42

my_float = 4.2

my_list = [1, 2, 3, 4, 5]

Python will assume the data type based on the type of value you assign, and whether you use special characters like quotation marks (for strings) or brackets (for lists). It’s important to keep track of data types, since Python can only perform certain kinds of operations on certain kinds of data.

In the example below, a and b are both assumed to be integers. Since integers are numeric data types, they can be added together:

a = 12

b = 13

a + b

We get:

25

However, if b is stored as a string through the use of quotation marks, b is no longer recognized as numeric. Consequently, the following:

a = 12

b = "13"

a + b

Returns an error, probably something like:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

We can access individual elements of sequential data types (like lists) by using index numbers to represent each place in the list. However, be sure to remember that Python counts index numbers from 0, rather than 1.

For example, to print the first element in my_list we use the variable name, followed by the index number 0 (not 1!) in brackets:

my_list = [1, 2, 3, 4, 5]

print(my_list[0])

And we get the "0th" element in the list:

1

Finally, note that indentation in Python is critical to the proper functioning of your code. This is different than in other programming languages, where indentation is often aesthetic, but does not make a functional difference. Indentations are white spaces on the left margin (either spaces or tabs).

As you’ll see in the module on For Loops, this code will work:

for x in my_list:
 print(x)

But this won’t:

for x in my_list:
print(x)

In the first example, the indentation tells Python that the print function occurs within the for loop. For each step through my_list, we print the value x.

In the second example, the lack of indentation suggests that the print function is not part of the loop, but rather follows it. Since nothing is indented after the for loop, nothing happens as we iterate over my_list and the for loop is viewed as incomplete, hence the error.

For more, see the w3schools Introduction to Python.