Download presentation
Presentation is loading. Please wait.
Published byAmberlynn Hart Modified over 5 years ago
1
Introduction In today’s lesson we will look at: why Python?
different types of Python program using IDLE getting started with Python – input and output
2
What is a Programming Language?
When you write a program, you use a special set of words as the instructions – these are called commands or keywords – and these make up the programming language. Examples of programming languages include BASIC, C and C++, Java, JavaScript, Pascal, PHP, Ruby, Perl – and Python! Luckily they are quite similar in that they mostly use the same ideas, and often a lot of the commands are the same, so it’s fairly easy to swap from one to another.
3
What Will We Use? You are going to use Python because that’s what’s been chosen by some of your schools. Python seems to be a popular choice for students following the new National Curriculum and also GCSE and A level Computing. It is free to download, and can be used for both text- based and form-based programs. The most common “development environment” for Python is called IDLE, but there are two versions of Python (2 and 3) so make sure you get the same one that you use in school.
4
Is Python the Best Language?
Actually, learning to program isn’t just about learning the programming language. All languages include things like: Variables Decisions Repetition Events The real skill is being able to take an idea for a program and to use these things to create it
5
Types of Application Most programming languages allow you to create:
Console applications – these are text-based programs, like the ones you might have created in Just Basic Form-based applications – these are applications with windows, buttons, etc. – the type that you’re more used to running on a PC. Python allows us to create both, and most commands will work with both, but we’ll start with console applications because they’re less fiddly.
6
Using IDLE You can use IDLE in one of two ways:
immediate/interactive mode: you can type commands one at a time and see what they do editor: you can create multi-line programs and save them in a file to run late When saving files… be sure to end the filename with .py IDLE doesn’t run all programs correctly; you (very occasionally) need to run programs by double-clicking the file
7
Can We Start Now? Looking at a very simple level, programs take an input, process it, and produce an output, so we’ll look at those key steps first. The command for output in Python is print() The keyword for input is input() [for numbers and raw_input() for text].
8
PRINTing You can print a number: print(123)
You can print text: print(“Hello”) You can print the result of a calculation: print(4*2) You can concatenate (join) words together: print(“Hello ” + “Everyone”) You can print “special” characters, e.g. newlines: print(“Hello\nWorld”) Each print() command starts a new line.
9
print(“My age is”+str(21))
PRINTing The examples on the previous slide work in both Python 2 and Python 3 Python 2 doesn’t need the brackets, and allows you to mix data types using a comma, e.g. print “My age is”, 21 The Python 3 equivalent would be: print(“My age is”+str(21)) You probably won’t find that many other differences – it’s a shame it’s the first lesson!
10
INPUT When you ask a user for input, you need:
A question, or prompt, to tell them what you want A variable to store the response We will look at variables in more detail next week, so don’t worry about this too much for now. Again, there is a difference in how Python 2 and Python 3 handle input.
11
INPUT or RAW_INPUT? Python 3: name = input(“What is your name?”)
age = input(“How old are you?”) Python 2: name = raw_input(“What is your name?”) This apparent complication does have benefits, though – you know what type of answer to expect
12
Comments Sometimes you want to include some text in your program – like a heading – that doesn’t do anything, but you don’t want it to cause an error Such text in a program is known as a comment There are two types of comment in Python… Comments on a single line start with a # Multi-line comments are surrounded by triple- quotes – i.e. ‘’’ or “”” Use comments to describe what your program does
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.