Session 1 SCRIPTING LANGUAGES 08-10-2018 WEN-CHI YANG
Your first program
Find the file 2. Click here 3. Copy the path 1. Find the file
Find the file 4. Go to the folder where your file locates by using cd Check if your file is here by using ls
Find the file 5. type python <filename> to execute your program
REPL READ-EVALUATE-PRINT-LOOP
Read-Evaluate-Print-Loop ➢ An interactive program ➢ Useful for testing short snippets of Python code ➢ Type quit() to stop REPL
Classroom Exercises
Comments ➢ Everything behind # is ignored ➢ Exercise 1
Data types ➢ Booleans : True , False ➢ Integers : …, -2, -1, 0, 1 , 2, 3, … ➢ Floating-point numbers : 1.0, 0.33, -3.14159 , …. ➢ Strings : “Hello world!” ➢ Exercise 2
Boolean Operators ➢ not True is False , not False is True ➢ P and Q is True only when both P and Q are True ➢ P or Q is True when either P or Q is True P Q not P P and Q P or Q F F T F F F T T F T ➢ Exercise 3 T F F F T T T F T T
String Formatting ➢ %s for strings ➢ %d for integers ➢ %f for floating numbers ➢ \n inserts a line break, \t inserts a tab ➢ Exercise 4.1 & 4.2
Datetime ➢ today = datetime.date.today() # an object ➢ today.day # an attribute ➢ today.month ➢ today.year ➢ Similar for datetime.datetime.now() ➢ exercise 5.1 & 5.2
String concatenation ➢ Use + to concatenate strings, e.g. “hello” + “ world!” ➢ Try to print a digit with %d, %2d and %02d ➢ Observe ➢ Exercise 6
Challenge exercise: painting
Recommend
More recommend