UE 2V315 - "Initiation a l'informatique et a l'abstraction en biologie" (UPMC Paris 6)
Cheat Sheet
#Variable declaration: The variable initiation automatically includes declaration in python
# Variable types
valBool = True
valEntier = 0
valReel = 0.0
valStr = "Hello World!"
myList = [0,1,2,3]
# type(variableName)
type(valEntier) # > Integer
# String modifications
stringComb = "String1 = " + string1 + " and string2 = " + string2
stringVar = "You can insert variables in a string: digits = %d and strings = %s" %(valEntier, valStr)
# List modification and access
valStr = "a,e,i,o,u,y"
myList = valStr.split(",")
myList.append('a')
myList[2:4]
# Print and input
print(stringVar)
print("Hello World!")
# Input will initiate/update variable with info from the keyboard (by definition a text string)
valFirstName = input("First Name = ")
# Need to reformat variables (Entier and reel)
valAge = input("Age = ")
valAge = int(valAge)
# Random values
import random
dee = random.randint(1,6)
reel = random.random()
# Other library import options and their use
import random as rand
dee = rand.randint(1,6)
from random import *
dee = randint(1,6)
# Boolean codntions
# =, !=, >, <
# Complex: and, or, not
# Conditional selection
if <condition1> :
code1
elif <condition2> :
code2
else :
code3
# Conditional loop
#while <condition> :
# code
# code changing <condition> (e.g. i = i + 1)
while i < 5 :
print(myList[i])
i = i + 1
# Iterative loop
for a in myList :
# code using a
print(a)
Making a Graphical User Interface (GUI)
# Of note, the code below can be modulated to include only parts of the code (comment out (#) lines which you do not need - e.g. the checkbox section). If certain sections are removed some variables may no longer be defined and this has to be corrected in the output section after the root.mainloop() line.
# Import required functions to make a GUI (the tkinter module)
from tkinter import * #Python3
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
# Function defining a global variable and assign the path of a file to this variable
def selectFile():
global filename
filename = askopenfilename()
return(filename)
def doNothing():
print("OK, ok I won't")
# Opens empty window (hereafter named root) and sets the title
root = Tk()
root.title("My Title")
# User text Entry (can be transformed to numbers or a list)
UserInput = StringVar()
L1 = Label(root, text = "User input (text) :")
L1.grid(row=0,column=0,padx=5,pady=5,sticky=E)
E1 = Entry(root, bd = 5, textvariable = UserInput)
E1.grid(row=0,column=1,padx=5,pady=5,sticky=E)
# User boolean Entry (Checkbox)
userInputBool = IntVar()
C1 = Checkbutton(root, text = "User input (boolean)", variable = userInputBool, \
onvalue = 1, offvalue = 0, height=5, \
width = 20)
C1.grid(row=1, column=1,padx=1,pady=1)
# Button - selectFile
filename = ""
B1 = Button(root, text = "Select File", command = selectFile)
B1.grid(row=2,column=1, padx=5, pady=5, sticky=S)
# Button - Submit
B2 = Button(root, text = "Submit", command = root.destroy)
B2.grid(row=3,column=1, padx=5, pady=5, sticky=S)
# Ensures that the root window remains open until root.destroy() is called with the "Submit" button.
root.mainloop()
# Return user entry in Terminal
userInputStr = UserInput.get()
print("User input (text) = ", userInputStr)
userInputBoolVal = userInputBool.get()
print("User input (boolean) = ", userInputBoolVal)
print("Filename = ", filename)
# Return user entry in window
# hide main window
root = Tk()
root.withdraw()
# Make messagebox with userInfo
messagebox.showinfo("UserInput","UserInputStr = " + str(userInputStr) + "\nuserInputBoolVal = " + str(userInputBoolVal) + "\nFilename path = " + str(filename))
root.destroy()