Download presentation
Presentation is loading. Please wait.
1
Advanced Coding Session 5
Royston Shufflebotham
2
The Story So Far… Created our own blocks in Scratch We can run Python
Variables Arithmetic Errors Opening, Finding, Saving Files Python Turtle Graphics
3
Today Python Creating our own Python Programs from scratch
Variable names Blocks & Nesting Conditionals (if)
4
Quick Python Shell Recap
Start up Python Use the IDLE/Shell window to perform a simple calculation: height = 4 width = 5 width * height I know this is ridiculously straightforward, but we’re about to do something different, and I want people to see the big difference between the Shell and a program first hand.
5
A New Saved Program! Save your program (File->Save)
Task: Create a new Python program, Save it, Run it Create a new file (File->New File) Type in our simple program: height = 4 width = 5 width * height Save your program (File->Save) in your own named folder in Documents with the file name first (It will save it as first.py) Run it (Run->Run Module) - what does it produce? What happens? Nothing! When you type commands into the shell, Python tells you the results for every one. In a program, it doesn’t. We need to print the result.
6
It didn’t do anything!? In the Shell window, Python calculates everything we ask it to, and tells us the answers immediately In a saved Python program, it doesn’t: we need to tell Python when to print something to the screen Task: use a print instruction in your program: print(width * height) Run it again. Any better? I’m going to start calling the IDLE/Shell window just the ‘Shell’ window from now on. It’s the window where you can type in bits of Python and get immediate answers. And we’re going to be jumping about between file editing Windows and the Shell window, so get used to that!
7
IDLE Help IDLE helps you as you type:
Did you notice IDLE helping you as you typed ‘print’? After you type the open bracket, it’ll give you whatever help it can find on that function. As you type functions in IDLE, it’ll try to give you help on the functions you’re using. It can look a little cryptic, but we’ll learn more about them later, and they’ll become very useful. For now, just notice that it gives you confirmation that you’ve got the function name spelled correctly.
8
Python – Variable Names
Can’t just pick any name for a variable in Python like you can in Scratch! Names can Contain letters (uppercase and lowercase) score Contain numbers player2score Contain underscore (‘_’) alien_speed Names can’t Contain anything else! Including spaces! Start with a number Be a ‘reserved’ word Also, Capital letters matter height and Height are different variables! (Normally start lower case in Python, i.e. prefer height) Make sure you know how to type an underscore (SHIFT+-) – you’ll use them a lot!
9
Python – Good Name, Bad Name Quiz!
Are these variable names legal (allowed) or illegal (not allowed)? Width score3 player_score 3rd_name width playerScore PLAYER_SCORE player-score player score If else Good Python Style! “playerScore” is interesting. It’s a common way of naming things in other languages such as JavaScript, JavC, C++, C#, etc and is called ‘camelCase’. It’s not used all that much in Python though. player_score is in a case known as ‘snake case’ PLAYER_SCORE is ‘screaming snake case’, and is used as a convention for names of constants in Python. player-score is ‘kebab case’ “if” would be illegal, but “If” is legal, and Python cares about case. But, whilst legal, “If” would be a really bad name for a variable!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.