Download Python_lesson - Computing4School

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

List of unsolved problems in philosophy wikipedia , lookup

Transcript
Lesson 2
Get Started with Python – Post-Installation – Use the GUI
Can I make a game now?
 Not yet!
 Basic programming concepts must come first.
 These concepts are:
values, operators, expressions, and variables.
Peter Vesterbacka – Co-Creator of Angry Birds
Angry Birds wasn’t
developed in a day ….
Everybody has to start
 Somewhere 
So let’s get Started With Python
 Assuming you are working in Windows,
you should be able to run Python by
choosing:
 Start, Programs, Python 3.1, IDLE (Python
GUI).
The Python Shell
Printing things to the Screen in Python
Print (‘Hello I wish to print this to the screen’)
In computer science,
the syntax of
a programming language is
the set of rules that define
the combinations of
symbols that are considered
to be correctly
structured programs in that
language.
Doing rather basic Maths Things in the Shell
Stuff like  2+2 = 4
Using the Shell like a Calculator!
Notice the space
here didn’t make
much of a
difference
Yeah – that can’t
happen, even in
Python.
Math Operators in Python
A list of math operators in Python.
47 + 2
addition
47 - 2
subtraction
47 * 2
multiplication
47 / 2
division
Expressions
 Expressions are made up of values (the numbers)
connected by operators (the math signs)
Sounding Clever
 When a computer solves the expression 3 + 5 and gets
the value 8, if we want to sound clever we say it
has evaluated the expression.
 Evaluating an expression is basically reducnig the
expression to a single value
 So, don’t say: “1+1 is 2 duh!” …say
“The expression 1+1 evaluates to the value 2 ….”
Creating a Chatbot!
Open a new Window !
If you’re lazy, Cut and paste
question = "What did you have for lunch?"
print (question)
answer = input()
print ("You had " + answer + "! That sounds
delicious!")
Type this code in!
If you’re lazy, cut and
paste the below code.
Click File – Save as.
Save the file, and as
long as it ends with .py
It will work okay!
Run the program and
test it to see if it works!
Using the given code and
analysing how it works, are
you able to ask more
questions and get more
answers, and thus create
your own chatbot?
http://www.codecademy.com/
Click on “LEARN”
Select Python!
See what more you can
learn!
And back to Errors for a while….
Let’s look at the types of Errors in Programming and what they are -
What was the last mistake you
made?
We all make mistakes ….
Sometimes we say or do the wrong things….
Other times, our thinking (or logic) is flawed
which leads to incorrect conclusions.
And we all know that when we don’t follow good advice, bad
things tend to happen.
Mistakes are part of life ….
But following instructions (or good laws) can help minimise
them.
In the human body, pain acts as a
warning signal when something is
wrong.
 Put your hand in
a flame and the
pain will send a
signal to your
brain making you
instantly pull
away.
In a disease called Leprosy, the
patients cease to feel pain, so their
limbs waste away and get damaged.
 Warning signals are
important!
 In computing
language we call this
VALIDATION
 Validation messages
can pop us to tell us
when we’ve made a
mistake.
Image: the hands of a leprosy victim
Warning signals or helpful warnings to
VALIDATE data in Computing
 Validation =
ensuring that all
the data entered
into the system is
VALID
 It doesn’t have to
be “correct” but
must be valid.
Restricting options are a
type of validation as the
user is stopped from
making a mistake!
Errors in Programming
 Types of Errors
1. Syntax Errors
2. Run Time errors
3. Logic Errors
As a programmer – you may come across the above types of errors.
They are errors of different types and are usually a result of mistakes
that have been made. Mistakes or errors occur when someone hasn’t
been careful to follow the RULES of the language they are using ….
The following sentence:
I green the monkey.
Is grammatically wrong or has what we call a syntax error.
"Green" is an adjective, not a verb. Errors like this (or even
misspellings) can’t be understood. They are nonsense!
Similarly, if you enter a VB statement that the compiler
cannot understand, you have committed a syntax error.
Run time errors are detected while the program
executes. They’re the ones that CRASH your program
These are often discovered when the user enters
illegal data.
For example, if the user types in a string when the
computer expects a number, there will be a runtime
error.
Runtime errors can be tricky to track down. These
errors occur when the programme is running. A
simple way to crash a programme is to
try to divide by zero!
Syntax errors are errors of grammar; whereas,
logical or semantic errors are errors of
meaning. These can be thought of as coding
errors. Coding errors that YOU make.
They can be quite difficult to track down, and
have you pretty annoyed. You will often hear
yourself saying “It should work! Why won't
it?!?!"
Example Source: http://www.homeandlearn.co.uk
Example Source: http://www.homeandlearn.co.uk
Heard of Ludwig Wittgenstein?
Considered among the
greatest philosophers of
the century. He studied
the problems and
limitations of language!
The Tractatus LogicoPhilosophicus
 In the Tractatus,
Wittgenstein attempts to
acquire an understanding
of how language works. He
believes that before we
attempt to solve the
problems of philosophy,
we must first understand
our use of language, and
how it relates to the world
we observe
A lot of Great Computer Scientists …
 …were also Great Philosophers
 Take the Great CHARLES BABBAGE
(inventor of the first analytical
engine)
 He pontificated a lot about the
nature of the universe
 In some writings, he wrote of
“God” as the supreme programmer
– and suggested the world we live
in may just be a program!
 We’ll be looking at Variables (how to implement these in
Python) next …
End of Lesson 2