170 likes | 279 Views
In this lesson, students will learn how to set up and use variables in Python programming. Through practical examples, they will explore the differences between interactive and script modes in IDLE, understand types of variables including integers, strings, and floats, and discover naming conventions and common errors associated with variable usage. The agenda includes reviewing Python basics, completing a grade sheet, and assignments such as the Number Guessing Game. This foundational knowledge is essential for writing effective and error-free code.
E N D
Student objective: How are variables set up and used in programming? Bell ringer: Where we using python in the interactive mode or script mode on Friday?
Set Up Grade Sheet • KWL (5) • Syllabus (5) • Communication compact (5) • Computer Parts Project (25) • Vocab (10) • Print Project (10) • Number Guessing Game (10)
Agenda • Notes on python basics and variables • Assignments: programming
Bell Ringer Answer: Interactive Interactive mode-program and run in IDLE • Immediate feedback • Not designed to save and run later Script Mode Write the program • Write program and save it to use later
Let’s try it • To write a python program and save it: • Open Python IDLE • Click on file—New Window • Type your program in • File—Save as .pyin documents folder of python • Click Run—Run Module • IDLE pops back up with program
Syntax Errors • Spelling/grammar • Picked up prior to running program
Runtime errors • Occur during execution of program • 4-line error message starting with Traceback • Tells where the error is and what it is
Semantic errors • Don’t use commas with numbers or will get this error • Code will run but be wrong
Computer • Input • Processing • Output
Variables Variables (values) • Integers: int numbers no decimal points • Strings: str use singleor double quotation character or string of characters: letters, numbers, punctuation • Long string: str multi-line string: triple quotes • Floating point: float decimal numbers
Color coding • Special words, like print: orange • Strings: green • Output: blue • Comments: red
Comments • Used to explain code • Invaluable to programmers • Are not executable by the computer • # symbol is used to denote the start of a comment
Blank Lines and Spacing Blank lines • Are ignored by the computer • Can make program easier to read Spacing • is critical and will create errors if not followed • Tells where blocks of code start and where they stop • Convention: 4 spaces in • Necessary for if statement
Naming Variables • If you don’t know the type of variable, you can find out by • Writing in the IDLE: • type ( variable ) then click return (Remember strings in quotes, numbers not) • You can name variables (words, numbers) to input into memory (Name it so you can use it) • Teacher = “Mr. Morton” • First = 5 • *You can have more than one name for one thing • *You can also change names
Rules for naming variables: • Can use letters, numbers and underscore character(_) • Case sensitive • Must start with letter or underscore (Not a good idea to start with underscore), NOT A NUMBER • Make variables names clear—don’t try to abbreviate
Concatenate: • Adding strings like “cat” + “dog” • Results would be catdog
Using variables • Variables can also be made to equal themselves: • >>Score = 7 • >>Score = Score • >>Score = Score + 1 • >>print Score Hit return • Generates >>8 • Useful to increment or decrement a value