Advanced Programming with Python
DISCLAIMER: The presented material relies heavily on Python Advance course carried out at CERN. The material is also available freely at
the website: https://www.python-course.eu (https://www.python-course.eu)
1. What is a variable
2. Basic types
string
enum
3. Containers
lists
tuples
sets
dictionaries
4. Functions
arguments
recursion
static variables
decorators
generators
context managers
5. Exception Handling
Not included:
6. Object Oriented Programming
7. Packaging
8. Documentation
9. Unit testing
10. Continuous Integration
In 1999, Guido Van Rossum submitted a funding proposal to DARPA called "Computer Programming for Everybody", in which he further
defined his goals for Python:
An easy and intuitive language just as powerful as major competitors
Open source, so anyone can contribute to its development
Code that is as understandable as plain English
Suitability for everyday tasks, allowing for short development times
0. Hello world
In [1]:
print('Hello world!')
Hello world!
0.1. Zen of Python
,In [2]:
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
1. What is a variable?
Variable in python is always a reference to an object as in python everything, even a function, is an object.
In [3]:
x = 3
y = x
y, x
Out[3]:
(3, 3)
In [4]:
x = 2
In [5]:
y, x
Out[5]:
(3, 2)
Conditional statement to assign a value
In [6]:
x = -5
if x > 0:
label = 'Pos'
else:
label = 'Neg'
print(label)
Neg
,In [7]:
x = -5
label = 'Pos' if x > 0 else 'Neg'
print(label)
Neg
In [28]:
print('Pos' if x > 0 else 'Neg')
Neg
2. Basic types
2.1. String
Strings in python are immutable
TypeError: 'str' object does not support item assignment
In [15]:
string.replace('M', 'T')
Out[15]:
'Ty string'
In [16]:
string
Out[16]:
'My string'
String is iterable
In [17]:
for s in 'My string':
print(s)
M
y
s
t
r
i
n
g
Formating of strings
, In [18]:
from datetime import date
'Today is ' + str(date.today()) + '.'
Out[18]:
'Today is 2019-11-28.'
In [23]:
'Today is {} and number {}.'.format(date.today(), [1, 2, 3])
Out[23]:
'Today is 2019-11-28 and number [1, 2, 3].'
f-strings have been introduced in Python 3.6
In [21]:
print(f'Today is {date.today()}')
Today is 2019-11-28
Check if a substring is in a string
In [25]:
if 'sub' in 'substring':
print('True')
True
There are already many built-in functions for handling strings in Python
In [29]:
dir(list)
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller rojengokul. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $9.59. You're not tied to anything after your purchase.