Download Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Programming for language
technologists I (fall, 2015)
Syllabus
Learning outcomes
On completion of the course, to earn the grade Pass the student should at least be able to do the
following:
(1) write programs applying good practices concerning design and coding;
(2) explain the following concepts and write programs making use of them:
• variables
• functions
• modules
• numerical types and operators
• lists and tuples
• conditional control (if-statements)
• iteration
• strings and operations on them
• dictionaries
• text file input and output
• regular expressions;
(3) write programs which solve some elementary language technological problems.
Lectures and supervision
• 5 weeks
• ~10 h/week lectures + lab time
• Teachers:
– Part I (until Sep. 16): Sofia Cassel
– Part II (from Sep. 17): Marcin Włodarczak
– Lab instructor: Nils Blomqvist
Assignments and grading
• Assignments determine your grade (no exam)
• Grades: A-B-C-D
• 6 sets of assignments:
– some are not graded
– some are graded
– some are graded, but only mandatory if you want
a chance at grade A or B
• More details on course webpage!
Part I of this course
Teaching
• Python basics (now-Sep. 2)
• Control (Sep. 3-9)
– Conditionals
– Functions
– Iteration
• Complex data (Sep. 10-16)
–
–
–
–
Strings
Lists
Tuples
Dictionaries
Deadlines
• Assignment set 1 (Basics)
due Sep. 7
• Assignment set 2 (Control)
due Sep. 14
• Assignment set 3 (Complex
data) due Sep. 21
Hand in via email
Instructions on course
webpage!
We expect you to…
•
•
•
•
•
Read the book
Ask questions in class
Find information online (Google is your friend)
Play around with Python
Discuss with your classmates
BUT: do the assignments on your own
Programming for language
technologists I (fall, 2015)
Let’s get started.
Programming: a Very Brief overview
• many different languages
• high-level vs low-level
• interpreted vs compiled
Python
• high-level
• interpreted
• focus on readability
Interpreted language
another program (the
interpreter) reads your
code and executes it
directly
Compiled language
another program (the
compiler) reads your
code and translates it to
machine-specific code
Different ways to do the same thing
Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, world!”);
}
}
LOLCODE
HAI
CAN HAS STDIO?
VISIBLE “HAI WORLD!”
KTHXBYE
Python
print(“Hello, world!”)
BASIC
PRINT “Hello, World!”
C
#include <stdio.h>
int main(void) {
printf(“hello, world\n”);
}
<!DOCTYPE html>
<html>
<head></head>
<body>
<p>Hello, world!</p>
</body>
</html>
https://en.wikipedia.org/wiki/List_of_Hello_world_program_examples
HTML
Today’s goals
• Be able to run IDLE on suitable computer
• Be familiar with variables, values, types, and
assignments.
• Be familiar with operators, operands, and
expressions.
• Know how to write small pieces of Python
code.
• Get started with assignment set 1.
Play with IDLE for the first time!
Open the interpreter in interactive mode (IDLE).
You’ll see a few lines of text, then the prompt:
>>>
Type in the “Hello, world!” program:
>>> print(“Hello, world!”)
Don’t do this:
(Why not?)
>>> >>> print(“Hello, world!”)
^
SyntaxError: invalid syntax
Variables, types, and values
Types are categories of values
Integer
String
0
1
23
9879707530
-4
…
“1”
“My monkey”
“Oo000ps!”
“ajlkfdjoi”
“”
“ “
…
Floating-point
1.0000000
23.12
-0.0
0.987970753
…
(more types later in this course!)
Variables, types, and values
Variables are used to store values
assignment operator
variable name
value (of type integer)
>>> alexander = 4
Not all words can or should be used as variable names:
• Do not use Python’s keywords
• Use intuitive variable names!
Variables, types, and values
>>> type(34)
<class ‘int’>
Q: What type of value is 34?
A: 34 is an integer.
>>> x = 34
Assign the value 34 to the variable x.
>>> type(x)
<class ‘int’>
Q: What type of value is stored in x?
A: x stores an integer.
>>> print(x)
34
Q: Please print whatever is stored in x!
A: the value 34
Variables, types, and values
>>> “x” = 48
>>> x = “48”
>>> x
What do you think
will happen?
>>> type(58)
Why?
>>> type(y)
>>> type(58.0)
Play with IDLE
Assign values to variables. Ask IDLE what types they are.
How many ways can you come up with to represent the
concept ‘thirty-four’ using different types?
Don’t do this:
(Why not?)
… or this:
(Why not?)
>>> x = thirty-four
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘thirty’ is not defined
>>> class = 34
^
SyntaxError: invalid syntax
Operators
operator
operand (of type integer)
operand (of type integer)
>>> 3 + 4
7
result of
evaluating the
expression
expression
Operators
>>> 7289690.78 – 3.59
7289687.19
>>> x = 44
>>> x * 5
220
7289690.78 operand of type float
3.59 operand of type float
- operator
x operand of type integer
5 operand of type integer
* operator
…but not all operators work for all types!
>>> “pickaxe” – “axe”
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’
Statements vs expressions
Statement
• Tells the interpreter to do
something
• Has no value
>>> x = 4
Expression
•
•
•
•
Has a value
Can be a variable or value
Can be an expression…
Can be used as a statement
>>> x
4
Tells interpreter to store 4 under the
name x.
The statement x = 4 has no value
… but x is an expression that has a
value!
Operators
>>> z = 3 + 3
>>> w = “48”
>>> w + 8
What do you think
will happen?
>>> 58 + 0.05
Why?
>>> 0 - .01
>>> “x” * 4
Making bigger expressions
>>> 35 * 2
70
value of the expression
expression
value of the
new
expression
assignment statement
>>> apple = 3.1
>>> apple
3.1
value of the
expression
>>> (35 * 2) + apple
73.1
new expression
expression
Making bigger expressions
Combine different variables: >>> apple = 9
>>> pear = 28.0
>>> apple + pear
37.0
Combine different
expressions (the order of
operations matters!):
>>> apple = 9
>>> 3 + 7
10
>>> apple * 3 + 7
34
vs.
>>> apple = 9
>>> apple * (3 + 7)
90
Making bigger expressions
>>> z = 3 + 3
>>> z + 3 - 7
>>> w = “48”
>>> w * 3 + “0”
What do you think
will happen?
>>> 0 - .01 / 2
Why?
>>> a = 1 + 2
>>> b = 4 * a
>>> a + b
Play with IDLE
Write expressions using variables, values, and operators.
What operators can you use and what do they do? Try
them on values and variables with different types!
How can you find out the type of an expression?
Don’t do this:
(Why not?)
>>> 4 / 0
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ZeroDivisionError: division by zero
Interpreter modes
Interactive mode
Type commands directly
to interpreter
>>> x = 40
>>> x + 4
44
>>> print(x)
44
This is what we have
used so far!
Script mode
1. Save your code in a .py file
2. Run your code using
python myscript.py
x = 40
x + 4
print(x)
Output from running script.py
44
Review today’s goals
• Be able to run IDLE on suitable computer
• Be familiar with variables, values, types, and
assignments.
• Be familiar with operators, operands, and
expressions.
• Know how to write small pieces of Python
code.
• Get started with assignment set 1.