Download to Python for Pygame Workshop - School of Information Technologies

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

Functional programming wikipedia , lookup

Reactive programming wikipedia , lookup

Object-oriented programming wikipedia , lookup

CAL Actor Language wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Types of artificial neural networks wikipedia , lookup

Structured programming wikipedia , lookup

Python syntax and semantics wikipedia , lookup

Python (programming language) wikipedia , lookup

Transcript
Introduction to Python!
NCSS Workshop
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
2
Outline
1
Hello World
2
What is Python?
3
Variables
4
User Input
5
Practice!
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
3
So what is this “programming” thing?
• So, you all know what a program is - it runs on your computer,
and it does stuff. But what do you think it really looks like?
• When I was in high school I thought it might look something
like this:
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
4
It’s much nicer than that!
• Programming is more like setting out instructions or a recpie
for the computer to follow
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
5
It’s pretty easy to understand
Here is a short Python program that will ask a very simple
question:
1
2
3
4
5
>>> value = int(raw_input('What is 5 times 5? '))
... if value == 25:
...
print 'Correct.'
... else:
...
print 'Wrong!'
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
6
There are many different kinds of programming languages
Programming languages taught in schools include:
• Visual Basic (and Visual Basic .NET)
• C#
• Java
• Python
• ColdFusion
To run programs written in some of these languages you will need
a program called a compiler. The compiler checks that your
program is syntactically correct and then creates an executable
version that your computer understands.
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
7
We are going to learn Python
• Python programs do not need to be compiled - instead they
are run by the Python interpreter.
http://xkcd.com
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
8
Hello World is tiny in Python
• Hello World is the canonical first program to write
• In Python, Hello World is only one line!
• You can type it directly into the Python interpreter:
1
2
3
>>> print "hello world"
hello world
>>>
• The colours in this (and all examples) matches the syntax
highlighting that idle produces.
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
9
Hello World in idle
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
10
Python is a great calculator
1
2
3
>>> 100 + 3*12 - 6.0
130
>>>
• * is used for multiplication (like almost all languages)
• it follows correct precedence (3*12 happens first)
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
11
No Storing is boring
• We can use variables to save bits of information to use later
• Modern computers have lots of memory. Lots and lots:
for example, 4, 294, 967, 296 bytes.
• We could store our values at practically any of these addresses:
e.g. put the result of "hello"*3 in memory at 0x54fd0.
• Remembering addresses for one (or many) values is tough!
• Humans are much better at remembering names than numbers
because we can choose meaningful names for each location.
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
12
Variables can be used just like literals
1
2
3
4
>>> x + x
6
>>> x*5
15
• they can even be used to create new variables by assignment
5
6
7
8
>>> y = x
>>> y
3
>>>
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
13
Each variables stores a separate copy of its value
1
2
3
>>> x = 3
>>> y = x
>>> x = 5
• What do x and y contain now?
4
5
6
7
8
>>> x
5
>>> y
3
>>>
• So, changing x does not change y because y is holding a
separate copy of the integer
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
14
Interactive programs take input from the user
• We will use the raw_input() function
• This waits for input from the user.
1
>>> name = raw_input("What is your name? ")
• Here the program will wait until the user enters something.
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
15
Raw Input is returned as a string
• Python stores different types of data differently
• We sometimes need to cast between strings and integers
1
2
3
4
>>> number = raw_input("What is your favourite number? ")
>>> num = int(number)
>>> if num == 8:
...
print "Wow! " + number + " is my favourite too!"
• Here we have to cast the string to an integer to find out if it’s
equal to 8 or not.
NCSS Workshop
Introductory Python
Girls Programming Network
Hello World
Python
Variables
User Input
Practice!
16
Now put this into practice!
• Now you can do the NCSS Challenge question: Hello World!
NCSS Workshop
Introductory Python
Girls Programming Network