Python: Introduction
Index for the course:
- Introduction
- Control Flow: if, for, while.
- String operations. Input/Output.
- More looping and data structures: dict, set.
- Numpy and plotting.
- Recursion and dynamic programming
Grading
The lab reports are a training to write scientific texts and an important part of the course. To pass you need to provide a final version of the lab report within 7 days after the lab. You have a chance but it is not mandatory to submit a preliminary version and receive feedback on it. The preliminary version needs to be submitted within 72 hours of the lab and feedback will be provided at the latest 24 hours before the deadline of the assignment. If the final report it not submitted in time or it contains an error you will get an Fx on the lab course. This means that you have to submit an updated lab report within 7 days after the exam and that you will receive an E on the entire course. If this does not occur you will have to re-register for the course next time it is given (normally next year) and complete the missing parts that year and you can still not receive a grade higher than E.
Course reading
Most of the content of the course follows these tutorials, so feel free to follow whichever you like the most (or switch freely!). Some details may not be readily available and will require a bit of googling.
https://docs.python.org/3/tutorial/http://www.scipy-lectures.org/intro/language/python_language.htmlhttp://www.python-course.eu/python3_course.php
If you speak any programming language other than Python this may bring you up to speed fast.
https://developers.google.com/edu/python/
If you know of any other nice tutorial to be addedd to the list, the instructors and future students would welcome the suggestion!
Extra reading and exercises:
Alternative book, that probably covers most of the course:
https://learnpythonthehardway.org/book/
More exercises to try:
http://rosalind.info– Bioinformatics oriented exercises.http://www.pythonchallenge.com– Increasingly harder exercises, get creative!https://github.com/gregmalcolm/python_koans– A philosophical approach to learning programming (recommended as the course ends).
General recommendations:
- Don’t copy & paste code, retype it. This forces you to understand what is going on.
- When an error occurs, read it carefully. What has gone wrong? Why? Where? Keep in mind, errors may be triggered well after the actual problem happened!
- Test your code. Try to break it. Find corner cases. Make sure it does what you want it to do.
Session 1: Introduction
Content
- Variables and basic types
- Python as a calculator
- Reading exceptions
- Scripts and interactive mode
- Basic I/O (input/output)
- Function definition
Reading
https://www.python-course.eu/python3_interactive.phphttps://docs.python.org/3/tutorial/Chapters 3 + 4.6
Exercises
Only the mandatory should be handed in. The file name should be yourname_yoursurname_sess1.py. Ignore accents and special characters. Before submitting, please make sure the code is correct and send it to david.menendez.hurtado@scilifelab.se.
Don’t hesitate to ask us to clarify if you have any questions.
Introductory exercises
- Open Python. Type something random. Read the error and try to understand it.
- Try to provoke a
SyntaxError, aNameError, and aZeroDivisionError. Bonus points forValueErrorandIndexError! - What happens if you raise -1 to 0.5?
- What is the difference between a list and a tuple?
Mandatory exercises
- Write a program that prints “Hello, world” to the screen.
- Read in a file as a list of lines. Then, save the first and last lines to a new file. The name of the new file should be the same as the original, plus
_headtailSo, if your input file is calledfoo, the output should befoo_headtail - Write a function that takes two strings as input and prints them to the screen.
Additional exercises:
- Given a single digit N, make a program that computes
NNNN + NNN + NN + N. For example, ifN = 1, compute the value of1111 + 111 + 11 + 1. - Given the length of two sides of a right angle, compute the length of the hypotenuse.
- Then, given the hypotenuse and one side, compute the length of the missing side.
Question about the following example ( given on https://docs.python.org/3/tutorial/introduction.html Under headline 3.2)
>>> # Fibonacci series:
… # the sum of two elements defines the next
… a, b = 0, 1
>>> while b 1,1
a,b=1,1+1 =>1,2 and etc.
Thank you!