<jupyter_start><jupyter_text>.... Session 3 : Python Recap Session 3.2:  Basic Python Cheat Sheet- [Cheat Sheet](cheat_sheet_basic_python.ipynb) Variables<jupyter_code>this_is_a_string = "3"
print("this_is_a_string = {0}".format(this_is_a_string))

this_is_an_integer = 3
print("this_is_an_integer = {0}".format(this_is_an_integer))

this_is_a_string_converted_to_an_integer = int(this_is_an_integer)
print("this_is_an_integer + this_is_a_string_converted_to_an_integer = {0}".format(this_is_an_integer + this_is_a_string_converted_to_an_integer))<jupyter_output><empty_output><jupyter_text>For loopsThe **`for` loop** in Python iterates over each item in a collection (such as a list) in the order that they appear in the collection. What this means is that a variable (`colour` in the below example) is set to each item from the collection of values in turn, and each time this happens the indented block of code is executed again.<jupyter_code>all_colours = ['red', 'blue', 'green']
for colour in all_colours:
    print(colour)<jupyter_output><empty_output><jupyter_text>FilesTo read from a file, your program needs to open the file and then read the contents of the file. You can read the entire contents of the file at once, or read the file line by line. The **`with`** statement makes sure the file is closed properly when the program has finished accessing the file.Passing the `'w'` argument to `open()` tells Python you want to write to the file. Be careful; this will erase the contents of the file if it already exists. Passing the `'a'` argument tells Python you want to append to the end of an existing file.<jupyter_code># reading from file
with open("data/genes.txt") as f:
    for line in f:
        print(line.strip())
# printing only the gene name and the chromosome columns
with open("data/genes.txt") as f:
    for line in f:
        data = line.strip().split()
        print(data[0], data[1])<jupyter_output><empty_output><jupyter_text>Conditional executionA conditional **`if/elif`** statement is used to specify that some block of code should only be executed if a conditional expression evaluates to `True`, there can be a final **`else`** statement to do something if all of the conditions are `False`.Python uses **indentation** to show which statements are in a block of code.<jupyter_code># printing only the gene name and its position for chromosome 6
with open("data/genes.txt") as f:
    for line in f:
        data = line.strip().split()
        if data[1] == '6':
            print(data[0], data[2], data[3])<jupyter_output><empty_output><jupyter_text>Getting help[The Python 3 Standard Library](https://docs.python.org/3/library/index.html) is the reference documentation of all libraries included in Python as well as built-in functions and data types.The Basic Python [Cheat Sheet](cheat_sheet_basic_python.ipynb) is a quick summary based on the course ['Introduction to solving biological problems with Python'](http://pycam.github.io/).<jupyter_code>help(len)          # help on built-in function
help(list.extend)  # help on list function
help("a string".strip)<jupyter_output><empty_output><jupyter_text>To get help for the `split()` function, you can look at the [Python documentation]((https://docs.python.org/3/library/index.html)) and search for [`str.split()`](https://docs.python.org/3/library/stdtypes.html?highlight=splitstr.split)<jupyter_code>help("a string".split)
# help within jupyter
str.split?<jupyter_output><empty_output><jupyter_text>Packages and modules A package is a collection of Python modules: while a module is a single Python file, a package is a directory of Python modules containing an additional __init__.py file.Some packages are part of the default python download (eg. : the statistics package), some need to be installed (eg. : the biopython package).When a package is uploaded onto a PyPi archive you can install it by using pip in your bash : ```bashpip install biopython```To use a package in a python script, you need to import it :<jupyter_code>import statistics

mean = statistics.mean([1, 2, 3, 4, 4])

print("Mean = {0}").format(mean))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09619093539054967, 'fraction_numerical': 0.00626808100289296, 'mean_word_length': 5.248493975903615, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '616771', 'license': 'Unlicense', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1218, 'n_words': 480, 'path': '3-2_python_basics.ipynb', 'repo_name': 'ClaudiuCreanga/DataScienceSchool', 'size': '4148'}