Python Basics

The print() Function

print(23 + 7)

comments

#

Arithmetical Operations

#Multiplication
print(1.99 * 2)

#Division
print(6.99 / 3)

#power of
print(4 ** 2)

Syntax Shortcuts

Variable Names

These are the two syntax rules we need to follow when we're naming variables:

  1. We must use only letters, numbers, or underscores (we can't use apostrophes, hyphens, spaces, etc.).

  2. Variable names can't begin with a number.

Variable Types

You can check the type of any variable with the type function

Conversion Between Types

It's possible to convert a float to an integer — and vice versa. To convert an integer to a float, we can use the float() function:

To convert a float to an integer, we can use the int() function:

Round function

If we want to round off a number, we can instead use the round() function, which has more flexibility and can also round up:

Note that running the round() function doesn't change the value stored by a variable unless we assign the rounded value back to the variable:

Strings

In Python, we can create text by enclosing a sequence of characters within quotation marks (" "):

String Concatenation

When we have two or more distinct strings, it's possible to link them using the + operator:

Output

We can't perform arithmetical operations between strings and integers.

Multi-line Strings

we can also write strings over many lines using the triple quotation mark symbol (''' or """). Using triple quotation marks also allows us to use both single and double quotation marks without needing to escape them.

startswith

To find the posts that begin with either Ask HN or Show HN, we'll use the string method startswith. Given a string object, say, string1, we can check if starts with, say, dq by inspecting the output of the object string1.startswith('dq'). If string1 starts with dq, it will return True; otherwise, it will return False.

In the example above, the first print call gives us False because dataquest does not start with Data. The second print call prints True because dataquest does start with data. Capitalization matters.

Lists

This is how we can create a list of data points :

A list can contain both mixed and identical data types. A list like [4, 5, 6] has identical data types (only integers), while the list ['Facebook', 0.0, 'USD', 2974676, 3.5] has mixed data types. We can also have a list of lists.

List length

To find the length of a list, we can use the len() function:

List Indexing

Each element (data point) in a list has a specific number associated with it — this is an index number. The indexing always starts at 0, so the first element will have the index number 0, the second element will have the index number 1, and so on.

To quickly find the index of a list element, identify its position in the list, and then subtract 11. For example, the string 'USD' is the third element of the list (position number 3), so its index number must be 2 since 3−1=23−1=2.

The index numbers help us retrieve individual elements from a list. Looking back at the list row_1 from the previous code example (shown below), we can retrieve the first element (the string 'Facebook') with the index number 0 by running the code print(row_1[0]).

Negative indexing

the last element has the index number -1, the second to last element has the index number -2, and so on

Subset a list

We, can select multiple items of a list and create a new list from it.

List slicing

  1. We retrieve the list slice we want by using the syntax a_list[m:n], where the following are true:

    • m represents the index number of the first element of the slice

    • n represents the index number of the last element of the slice plus one (if the last element has the index number 2, then n will be 3, if the last element has the index number 4, then n will be 5, and so on).

  2. When we need to select the first or last x elements (x stands for a number), we can use even simpler syntax shortcuts:

    • a_list[:x] when we want to select the first x elements.

    • a_list[-x:] when we want to select the last x elements.

Retrieving from List of Lists

The data_set variable is still a list, which means we can retrieve individual list elements and perform list slicing using the syntax we learned. Below, we'll do the following:

  • Retrieve the first list element (row_1) using data_set[0]

  • Retrieve the last list element (row_5) using data_set[-1]

  • Retrieve the first two list elements (row_1 and row_2) by performing list slicing using data_set[:2]

We'll often need to retrieve individual elements from a list that's part of a list of lists — for instance, we may want to retrieve the value 3.5 from ['Facebook', 0.0, 'USD', 2974676, 3.5], which is part of the data_set list of lists. Below, we extract 3.5 from data_set using what we've learned:

  • We retrieve row_1 using data_set[0], and assign the result to a variable named fb_row.

  • We print fb_row, which outputs ['Facebook', 0.0, 'USD', 2974676, 3.5].

  • We retrieve the last element from fb_row using fb_row[-1] (since fb_row is a list), and we assign the result to a variable named fb_rating.

  • We print fb_rating, which outputs 3.5

Above, we retrieved 3.5 in two steps: we first retrieved data_set[0], and then we retrieved fb_row[-1]. However, there's an easier way to retrieve the same value of 3.5 by chaining the two indices ([0] and [-1]) — the code data_set[0][-1] retrieves 3.5:

Above, we've seen two methods of retrieving the value 3.5. Both methods lead to the same output (3.5), but the second method involves less typing because it combines the steps we see in the first case. While you can choose either option, people generally choose the second one.

Append list function

Once we create a list, we can add (or append) values to it using the append() function

List functions

sum

length

Last updated