Python info

Documentation:

http://www.univ-irem.fr/videos/

https://docs.python.org/3/

Tutorials (learning Python):

https://realpython.com/

Basic examples :

  • Write a program that shows the current date and time
import datetime
x = datetime.datetime.now()
print(x)
  • Write a script that sums up numbers
x = 10.1

y = 2

z = 3

s = x + y + z

print(s)
  • Write a conditional block :
message = "hello there"

if "hello" in message:
    print("hi")
else:
    print("I don't understand")
  • Define a function:
def cube_volume(a):
    return a * a * a
  • The input halts the execution of the program and gets text input from the user. The input function converts any input to a string, but you can convert it back to int or float:
experience_months = input("Enter your experience in months: ")
  • The input halts the execution of the program and gets text input from the user. The input function converts any input to a string, but you can convert it back to int or float :
experience_years = int(experience_months) / 12
print(experience_years)
  • You can format stringswith (works both on Python 2 and 3):
name = "Thomas"
experience_years = 1.5
print("Hi %s, you have %s years of experience." % (name, experience_years))
  • Define a function that converts Celsius in Kelvin :
def celsius_to_kelvin(cels):
    return cels + 273.15
  • Loop using a function to obtain several results :
for letter in 'abc':
    print(letter)
monday_temperatures = [9.1, 8.8, -270.15]

for temperature in monday_temperatures:
    print(celsius_to_kelvin(temperature))
    • An *args parameter allows the function to be called with an arbitrary number of non-keyword arguments:
    def find_max(*args):
        return max(args)
    print(find_max(3, 99, 1001, 2, 8))
    

     

Output: 1001

  • An **kwargsparameter allows the function to be called with an arbitrary number of keyword arguments:
def find_winner(**kwargs):
    return max(kwargs)
print(find_winner(Andy = 17, Marry = 19, Sim = 45, Kae = 34))

Output: Sim

  • Use Dictionary – A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values. No duplicate numbers
thisdict = {
   "brand": "Ford",
   "model": "Mustang",
   "year": 1964
 }
print(thisdict)
y = thisdict["model"]
print(x)
y1 =  thisdict[0]
print(y1)
  • Loop over dictionary keys, values and both keys and values:
  • phone_numbers = {"John Smith":"+37682929928","Marry Simpons":"+423998200919"}
    for value in phone_numbers.keys():
        print(value)
    for value in phone_numbers.values():
        print(value)
    for key, value in phone_numbers.items():
        print(key, value)
    
  • Use List – A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
print(thislist[0:3])
  • While loops with break statement we can stop the loop even if the while condition is true, Exit the loop when i is 3:
 i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1  

i = 1
while i < 6:
   print(i)
   i += 1

File Processing

  • You can read an existing file with Python:
with open("file.txt") as file:
    content = file.read()
  • You can createa new file with Python and writesome text on it:
with open("file.txt", "w") as file:
    content = file.write("Sample text")
  • You can appendtext to an existing file without overwriting it:
with open("file.txt", "a") as file:
    content = file.write("More sample text")
  • You can both append and reada file with:
with open("file.txt", "a+") as file:
    content = file.write("Even more sample text")
    file.seek(0)
    content = file.read()

Example d’Algorithme simple,

n = 4

res = n – 4

res = res *4

res = res + 4

print (res)

faire tourner pour n compris entre 0 et 5 et observer les resultats la suite -4, 0, 4, 8, etc…

 

 

 

How to Launch MAC console :

 $ ipython --pylab

Modules of Interest:

Third-party librariesare packages or modules written by third-party persons (not the Python core development team).

Third-party libraries can be installedfrom the terminal/command line:

Windows:

pip install pandas

Mac and Linux:

pip3 install pandas

Statistics

https://docs.python.org/3/library/statistics.html
How to build histograms:
https://realpython.com/python-histograms/

Data analysis and Vizualization

use interactive python:

$pip install ipython

$pip install numpy

$ python m pip install U matplotlib

 

 

 

 

 

Tutorials – Examples

PIE

https://matplotlib.org/gallery/pie_and_polar_charts/pie_features.html#sphx-glr-gallery-pie-and-polar-charts-pie-features-py

BOITE a moustache

Box plot in Python with matplotlib

Writing mathematical expressions

https://matplotlib.org/users/mathtext.html

 

https://matplotlib.org/users/index_text.html

 

https://matplotlib.org/users/annotations.html#annotating-with-text-with-box

Book

https://learning.oreilly.com/library/view/python-for-data/9781449323592/ch03.html

https://learning.oreilly.com/library/view/python-for-data/9781449323592/ch08.html