390 likes | 522 Views
Lesson 2. Get Started with Python – Post-Installation – Use the GUI. Can I make a game now?. Not yet! B asic programming concepts must come first. These concepts are:. values, operators, expressions, and variables . . Peter Vesterbacka – Co-Creator of Angry Birds.
E N D
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.
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).
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. Correct Syntax Correct Syntax
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.
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 expression1+1 evaluates to the value 2 ….”
Step 2 Open a new Window !
Step 3 Type this code in! If you’re lazy, cut and paste the below code. 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!")
Step 4 Click File – Save as. Save the file, and as long as it ends with .py It will work okay!
Step 5 Run the program and test it to see if it works!
CHALLENGE: THINK! 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? Be Creative!
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 theirlimbs 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 • Syntax Errors • Run Time errors • 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 ….
Syntax Error The following sentence: I green the monkey. Is grammatically wrong or has what we call a syntaxerror. "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 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 errorscan 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!
Logic Error 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?!?!"
What type of Error? SYNTAX
What type of Error? LOGIC Example Source: http://www.homeandlearn.co.uk
What type of Error? RUNTIME Example Source: http://www.homeandlearn.co.uk
Heard of Ludwig Wittgenstein? The TractatusLogico-Philosophicus • 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 Considered among the greatest philosophers of the century. He studied the problems and limitations of language!
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 …