0% found this document useful (0 votes)
5 views6 pages

Python Programming Course Overview

This document is a course on Python. It contains many sections describing fundamental concepts of Python such as variables, lists, loops, and print functions. The document is organized pedagogically to gradually introduce the concepts.

Translated by

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

Python Programming Course Overview

This document is a course on Python. It contains many sections describing fundamental concepts of Python such as variables, lists, loops, and print functions. The document is organized pedagogically to gradually introduce the concepts.

Translated by

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

Python Course

[Link]

Patrick FuchsetPierre Poulain


prénom [point] nom [arobase] univ-paris-diderot [point] fr

version of September 7, 2018

University Paris Diderot-Paris 7, Paris, France

This document is under license


Creative Commons Attribution - ShareAlike 3.0 France
(CC BY-SA 3.0 FR)
[Link]
2 Python Course / University Paris Diderot - Paris 7 / UFR Life Sciences
Table of contents

1 Introduction 11
1.1 A few words about the origin of this course... 11
1.2 Acknowledgments 11
1.3 Before starting . . . . . . . . . . . . . . 12 . . . . . . . . . . . . . . . .
1.4 What is Python? 12
1.5 First contact with Python under Linux........................................................ 13
1.6 Premier Python program. 14
1.6.1 Call of the interpreter. 14
1.6.2 Direct call of the script ...14
1.7 Commentaires . . . . . . . . . . . . . . . .15. . . . . . . . . . . . . . .
1.8 Concept of instruction block and indentation 15
1.9 Python 2 or Python 3? 16

2Variables 17
2.1 Definition of a variable. 17
2.2 Types of variables 18
2.3 Naming of Variables 18
2.4 Operations 18
2.4.1 Operations on numeric types 18
2.4.2 Operations on character strings 19
2.4.3 Illicit operations. 19
2.5 The type() function . . . . . . . . . . . . . . . 20. . . . . . . . . . . . .
2.6 Type conversion 20
2.7 Note on the division. 20
2.8 Note on vocabulary and syntax 21

3 Display 23
3.1 Formatted writing. 23
3.2 Old method of string formatting 26
3.3 Note on vocabulary and syntax 27
3.4 Exercises. 27
3.4.1 Display in the interpreter and in a program... 27
3.4.2 Poly-A 27
3.4.3 Poly-A and poly-GC 27
3.4.4 Formatted writing. 27
3.4.5 Formatted writing 28

4 Lists 29
4.1 Definition 29
4.2 Utilization 29
4.3 Operation on lists... 30
4.4 Negative indexing. 31

3
Table of contents Table of contents

4.5 Tranches 31
4.6 Function len(). 32
4.7 The functions range() and list() . . . . . . . . . . . . 32
. . . . . . . . . . . .
4.8 Lists of lists 33
4.9 Exercises... 33
4.9.1 Days of the week 34
4.9.2 Seasons 34
4.9.3 Table of 9 34
4.9.4 Even numbers. 34

5 Loops and comparisons 35


5.1 For loops... 35
5.1.1 Principle 35
5.1.2 Function range() 37
5.1.3 Naming of the iteration variable. 37
5.1.4 Iteration over indices... 37
5.2 Comparisons 39
5.3 While loops. 40
5.4 Exercises 40
5.4.1 Basic loops. 41
5.4.2 Loop and days of the week 41
5.4.3 Table of 1 . . . . . . . . . . . . . . . . . . . . . . . 41. . . . .
5.4.4 Even and odd numbers . . . . . . . . . . . . . . . . . .41 . . . .
5.4.5 Calculation of average 41
5.4.6 Product of consecutive numbers 41
5.4.7 Triangle 41
5.4.8 Inverted triangle. . . . . . . . . . . . 42
. . . . . . . . . . . . . . .
5.4.9 Left triangle. 42
5.4.10 Isosceles triangle... 42
5.4.11 Matrix Route 43
5.4.12 Half-matrix path without the diagonal (exercise ++) 43
5.4.13 Flea jumps. 44
5.4.14 Fibonacci Sequence (exercise +++) 44

6Tests 47
6.1 Definition ...47
6.2 Tests with multiple cases 47
6.3 Multiple tests. 49
6.4 Instructions break and continue. 50
6.5 Value tests on real numbers . . . . . . . . . . . . 50 . . . . . . . . . . . . .
6.6 Exercises... 51
6.6.1 Days of the week 51
6.6.2 Complementary sequence of a DNA strand 51
6.6.3 Minimum of a list . . . . . . . . . . . . . . . . . . . 51. . . .
6.6.4 Frequency of amino acids. 51
6.6.5 Notes and mention of a student 52
6.6.6 Even numbers. 52
6.6.7 The riddle of Father Fouras. 52
6.6.8 Syracuse Conjecture (exercise +++). 52
6.6.9 Simple attribution of the secondary structure of the residues of a protein (exercise
+++). . . . . . . . . . . . . . . . . . . . .53. . . . . . . . . . . . . . . . .
6.6.10 Determination of prime numbers less than 100 (exercise +++). 53
6.6.11 Searching for a number by dichotomy (exercise +++) 54

4 Python Course / University Paris Diderot - Paris 7 / UFR Life Sciences


Table of contents Table of contents

7 Files 57
7.1 Reading from a file... 57
7.1.1 Method read(). . . . . . . . . . . . . 58 . . . . . . . . . . . . . .
7.1.2 Readline() method... 58
7.1.3 seek() and tell() methods. . . . . . . . . . . .59 . . . . . . . . . .
7.1.4 Iterations directly on the file... 59
7.2 Writing to a file ... 60
7.3 Optimized method for opening and closing files. 60
7.4 Note on carriage returns under Unix and under Windows 61
7.5 Importance of type conversions with files... 62
7.6 Exercises................................................... 62
7.6.1 Reading and line jump 62
7.6.2 Writing and line break 62
7.6.3 Secondary structures. 62
7.6.4 Spiral (exercise +++) 62

8 Modules 65
8.1 Definition 65
8.2 Importation of modules 65
8.3 Getting help on imported modules. 66
8.4 Current Modules. 67
8.5 Module sys: passing arguments. 68
8.6 Module os 69
8.7 Exercises. 70
8.7.1 Square root. 70
8.7.2 Cosine 70
8.7.3 List of files in a directory 70
8.7.4 Timed display. 70
8.7.5 Random sequences of digits 70
8.7.6 Random base sequences... 71
8.7.7 Date of birth. 71
8.7.8 Determination of the number pi by the Monte Carlo method (exercise 71+++). . .

9Functions 73
9.1 Principles and generalities 73
9.2 Definition 74
9.3 Passing of arguments... 75
9.4 Local variables and global variables... 76
9.5 Exercises... 80
9.5.1 Functions and pythontutor 80
9.5.2 Power function. 81
9.5.3 Pyramid function. 81
9.5.4 Prime number function. 81
9.5.5 Complementary function... 81
9.5.6 Distance function. 82
9.5.7 Distribution functions and statistics 82
9.5.8 Distance to origin function. 82
9.5.9 Area under the curve function (exercise +++) 82

10 Plus on strings 85
10.1 Preamble 85
10.2 Strings and Lists. 85
10.3 Special characters. 86

Python Course / University Paris Diderot - Paris 7 / UFR Life Sciences 5


Table of contents Table of contents

10.4 Methods associated with strings 86


10.5 Extraction of numerical values from a string... 88
10.6 Conversion of a list of strings into a string . . . . 88
. .
10.7 Exercises 90
10.7.1 Traversing a list of strings... 90
10.7.2 Frequency of bases in a nucleic sequence 90
10.7.3 Conversion of amino acids from the three-letter code to the one-letter code... 90
10.7.4 Hamming Distance 91
10.7.5 Palindrome. 91
10.7.6 Composable word. 91
10.7.7 Alphabet and pangram. 91
10.7.8 Display of alpha carbons of a protein structure. 92
10.7.9 Calculation of distances between consecutive alpha carbons in a protein structure 92

11 Plus on the lists 95


11.1 Properties of lists 95
11.2 Membership test... 97
11.3 Copy of lists 97
11.4 Exercises 99
11.4.1 Sort list . . . . . . . . . . . . . . . . . . . 99. . . . . . . . . . . . . . . . . . .
11.4.2 Random nucleic sequence. 99
11.4.3 Complementary nucleic sequence 99
11.4.4 Duplicates.................................................. 100
11.4.5 Random nucleic sequence 2
11.4.6 Pascal's triangle (Exercise +++). . . . . . . . . . . . . . . . . . . . . . . . . 100

12 More on functions 101


12.1 Calling a function within a function . . . . . . . . . . . . . . . . . . . . . . . . . . 101
12.2 Recursive functions
12.3 Scope of variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
12.4 Scope of lists.............................................. 105
12.5 LGI Rule.................................................. 106
12.6 Recommendations .............................................................................. 106
12.7 Exercises ................................................... 107
12.7.1 Predict the output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

13 Dictionaries and tuples 109


13.1 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
13.1.1 Iteration over the keys to obtain the values . . . . . . . . . . . . . . . . . . . 109
13.1.2 Methods keys() and values() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
13.1.3 Existence of a key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
13.1.4 List of dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
13.2 Tuples
13.3 Exercises
13.3.1 Composition in amino acids. . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
13.3.2 Words of 2 letters
13.3.3 Words of 3 and 4 letters
13.3.4 2 letter words of Saccharomyces cerevisiae..................112
13.3.5 Letters and GenBank files...
13.3.6 Letters of the E. Coli genome.
13.3.7 Dictionary and alpha carbon.
13.3.8 Dictionary and PDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
13.3.9 Barycenter of a protein . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

6 Python Course / University Paris Diderot - Paris 7 / UFR Life Sciences

You might also like