0% found this document useful (0 votes)
4 views18 pages

Python Data Types and Sequences Guide

The document provides an overview of various data types in Python, including strings, integers, floats, booleans, lists, tuples, and dictionaries, along with their usage. It explains how to manipulate sequences using indexing, slicing, and loops, and includes methods for counting, sorting, and accessing dictionary values. Additionally, it highlights the syntax for iterating over sequences and dictionaries in Python.

Uploaded by

mingqianli478
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Python Data Types and Sequences Guide

The document provides an overview of various data types in Python, including strings, integers, floats, booleans, lists, tuples, and dictionaries, along with their usage. It explains how to manipulate sequences using indexing, slicing, and loops, and includes methods for counting, sorting, and accessing dictionary values. Additionally, it highlights the syntax for iterating over sequences and dictionaries in Python.

Uploaded by

mingqianli478
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd

Annoying python

Delicious python
Delicious python
Delicious python
Data types#
In Python, the data type is set when we assign a value to a variable. Different data types can do different things.
The most common data types are
Strings (str) for text (surrounded by either single quotation marks or double quotation marks)
Integers (int) for whole numbers, positive or negative, without decimals, of unlimited length
Floating point numbers (float) for numbers, positive or negative, containing one or more decimals
Booleans (bool), with only two possible values: true and false
Lists (list) for multiple ordered and changeable items of different data types within one variable (created using square
brackets)
Tuples (tuple) for multiple ordered and unchangeable items of different data types within one variable (created using

Delicious python
round brackets)
Dictionaries (dict) for storing a collection of ordered, changeable, and non-duplicate data as “key : value” pairs (created
using curly brackets; pairs are separated using commas, and keys and values are separated using colons)
Use type(variable_name) to identify the data type of any variable.
Use len(sequence_name) to determine the length of a sequence (e.g. a string, list, or tuple).
Use sequence_name.count(value) to count the number of items with a specified value within a sequence.
Use sorted(list_name, key=myFunc (optional), reverse=True (optional)) to sort a list in ascending (by default), descending
(optional) or user-defined (optional) order. The original list variable is unchanged. Of note, strings are sorted in alphabetical
order based on their first letter (A-Z). However, words that start with uppercase letters come before words that start with
lowercase letters. For case-insensitive sorting, set the key argument to key=[Link], which converts all strings to lowercase
characters.
Use dictionary_name.get(key_name) to return the value of a specified key in a dictionary. If the key is not found, it returns
None.
Index and slice for sequence-based data types#
Sequence-based data types (e.g. a string, list, or tuple) are indexed, with _the first item having index 0. To select the first
item, use sequence_name[0]; to select the second item, use sequence_name[1] …
If we have a long sequence and want to select an item towards the end, we can count backwards, starting at the index
number -1.
The syntax for selecting a subset of an existing sequence, a slice, is: sequence_name[start:end]. When we specify the end
item for the slice, it goes up to but does not include that item of the sequence!
Use sequence_name[:end] to have the slice starting from the beginning of the sequence. Use sequence_name[start:] to
have the slice going to the end of the sequence.
The syntax for selecting a slice with regular steps, is: sequence_name[start:end:step]. A negative step goes backwards. For

Delicious python
example, use sequence_name[::2] to select every other element from the sequence, and use sequence_name[::-1] to select
the elements in reverse order.
res2019_protseqLRRK2 = protseqLRRK2[2018] #select residue 2019 (2018 as the first item has index 0)
kinase_protseqLRRK2 = protseqLRRK2[1878:2138] #select residues 1879 (1878 as the first item has index 0) to 2138 (the first item has index 0; however, it goes up to but does not inclu
Determine the data type, length, and number of tryptophan residues for this LRRK2 protein sequence containing one letter code
de item 2138)
amino variable
print("This acids. has data type", type_protseqLRRK2, ".",
Select residue
"The length of the 2019. The
sequence G2019S mutation
is", len_protseqLRRK2, in LRRK2
"residues .", is the most common genetic determinant of Parkinson’s disease identified to
"Trp appears", Wcount_protseqLRRK2, "times .",
date.
"Amino acid 2019 is a", res2019_protseqLRRK2, ".",
It"The
liessequence
in the protein’s
of the LRRK2kinase domain.
kinase domain Select this kinase
is", kinase_protseqLRRK2, ".")domain, itplace
#Print and includes residue
all values that we 1879 to residue
calculated 2138.
in a readable sentence. The print function can take more than
one object. By default, it separates objects with a space.

Delicious python
items in a sequence#
Working with sequences#
To execute a (set of) statement(s) once for each item in a sequence (e.g. a string, list, or tuple), we use a for loop. The
syntax for a for loop is:
for variable in sequence_name:
statement(s)
The variable accesses each item of the sequence on each iteration. After the sequence name, we find a colon (:). The
statement(s) is (are) indented using whitespace. The loop continues until we reach the last item in the sequence.
Use sequence_name.append(new_item) to save the outputs of the statement(s) in a new sequence: this function appends a
new item to the end of an existing sequence. Make sure to first create the new sequence outside of the loop!

Delicious python
Working with dictionaries#
To iterate over a dictionary, use:
for key, value in dictionary_name.items():
statement(s)
Of note, the items() method returns an object with key-value pairs of the dictionary, as tuples in a list.
Delicious python
Delicious python
Simple python
Delicious python
Delicious python
0 =
P
1 =
y
2 =
t
3 =
h
4 =

Delicious python o
5
n
=

[start:end:st
ep]

If step is 2 then every 2nd letter, if 3 then every


3rd letter
If step value is -ve then its reversed
[:] = all included
[0:] = all included
[:5] = only 0~4 included
Delicious python
Delicious python
Delicious python
Legends can be
(center/right/left/upper/lower)
Delicious python

You might also like