how to think like a computer programmer beginning python
TRANSCRIPT
![Page 1: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/1.jpg)
How to Think LikeHow to Think Likea Computer Programmera Computer Programmer
Beginning PythonBeginning Python
![Page 2: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/2.jpg)
OutlineOutline
•What Is Computer Science?What Is Computer Science?
•Computer ProgrammingComputer Programming
•Languages: Expressing Languages: Expressing things to the Computerthings to the Computer
•Fundamental ConceptsFundamental Concepts
•The Process of The Process of ProgrammingProgramming
•What Is Computer Science?What Is Computer Science?
•Computer ProgrammingComputer Programming
•Languages: Expressing Languages: Expressing things to the Computerthings to the Computer
•Fundamental ConceptsFundamental Concepts
•The Process of The Process of ProgrammingProgramming
![Page 3: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/3.jpg)
What is Computer Science?What is Computer Science?
• Not using a Not using a computercomputer
• Not just Not just programmingprogramming
• A field at the A field at the intersection of intersection of Math, Engineering, Math, Engineering, etcetc
![Page 4: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/4.jpg)
The AlgorithmThe Algorithm• Fundamental inventions in Fundamental inventions in
formal thinking:formal thinking:– Formal Logic (Athens, ~4 C. AD)Formal Logic (Athens, ~4 C. AD)– Mathematical Proof (Greece, Mathematical Proof (Greece,
~585 AD)~585 AD)– Secular Observation of NatureSecular Observation of Nature
(~600 AD)(~600 AD)– Arabic Numerals, including ZeroArabic Numerals, including Zero
(India, ~8 C. AD)(India, ~8 C. AD)– Algebra (al-Khwarizmi, ~810 AD)Algebra (al-Khwarizmi, ~810 AD)– Probability (late 1500's)Probability (late 1500's)– Scientific Method (early 1600s)Scientific Method (early 1600s)
• Now: Now: The AlgorithmThe Algorithm
![Page 5: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/5.jpg)
Computer ScientistsComputer Scientists• Who are Computer Scientists?Who are Computer Scientists?
• Computer Scientists study Computer Scientists study lots of thingslots of things– e.g., "What are Algorithms e.g., "What are Algorithms
Theoretically Capable of?Theoretically Capable of?““– Can Machines Think?Can Machines Think?
• Today we're going to look at Today we're going to look at the applied version: the applied version: ProgrammingProgramming
![Page 6: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/6.jpg)
Computer ProgrammersComputer Programmers
““Their rumpled clothes, their Their rumpled clothes, their unwashed and unshaven faces, and unwashed and unshaven faces, and their uncombed hair all testify that their uncombed hair all testify that they are oblivious to their bodies and they are oblivious to their bodies and to the world in which they move. to the world in which they move. These are computer bums, These are computer bums, compulsive programmers.compulsive programmers.””
![Page 7: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/7.jpg)
Computer Science as the New Computer Science as the New LiteracyLiteracy• The modes of thought that come from CS The modes of thought that come from CS
are influencing a huge number of fieldsare influencing a huge number of fields
• Modeling & SimulationModeling & Simulation– We can create all sorts of worlds inside the We can create all sorts of worlds inside the
computer to work withcomputer to work with
![Page 8: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/8.jpg)
What does a computer What does a computer do?do?• Computers internally just keep doing the Computers internally just keep doing the
same thing over and over:same thing over and over:– Get the next instructionGet the next instruction– Do whatever it saysDo whatever it says– Go back to step #1Go back to step #1
• But its internal instructions are in binary -- But its internal instructions are in binary -- too hard for people to understand easilytoo hard for people to understand easily
![Page 9: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/9.jpg)
Simplifying how we instruct the Simplifying how we instruct the machinemachine
High-level language
Assembly Language
Machine Code
Compiler
Assembler
Matrix::Compute(double* values, int size) { for(int i=0; i<size; i++) { for(int j=0; j<size; j++) { if(i<j && values[i*size+j]<0.0) values[i*size+j] = values[j*size+i]; values[i*size+j] = 2*values[i*size+j]; }
pushl %ebpmovl %esp,%ebpmovb hi_temp,%aladdb lo_temp,%almovb $0,%ahadcb $0,%ahmovb $2,%blidivb %blmovb %al,av_templeaveret
100101010110101101010101001010101011110100001101010011101010111010110001101010010011010101010101010101101111010101010100111101010101010111010101010110111010101101101011010111010001010100001010101011000100001010101010111110101010101011111111
![Page 10: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/10.jpg)
Natural LanguagesNatural Languages
• Computers donComputers don’’t understand Englisht understand English
• Need to deal withNeed to deal with– AmbiguityAmbiguity– RedundancyRedundancy– LiteralnessLiteralness
• So, we express what a computer should So, we express what a computer should do in a do in a formal languageformal language
![Page 11: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/11.jpg)
Formal LanguagesFormal Languages• A program usually just a A program usually just a
document -- a written list of document -- a written list of instructionsinstructions
• You saw some of this with You saw some of this with HTML -- A markup language is HTML -- A markup language is sort-of a programming sort-of a programming languagelanguage
Poetry: Ambiguity and metaphor are important
Prose: Literalness and structure important
Program: Formal, precise structure
![Page 12: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/12.jpg)
PB&J
Robot…
![Page 13: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/13.jpg)
Programming LanguagesProgramming Languages
• Some simple ones are built Some simple ones are built in to programs you use in to programs you use every dayevery day– Excel is a programming Excel is a programming
language (and contains language (and contains another)another)
– Word Macro LanguageWord Macro Language
• There are also General There are also General Purpose Programming Purpose Programming LanguagesLanguages
![Page 14: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/14.jpg)
PseudocodePseudocode
• There are lots of details to programming There are lots of details to programming languageslanguages
• WeWe’’ll also use something called ll also use something called ““pseudocodepseudocode””
• Most of the way to a programming languageMost of the way to a programming language– Excludes some detailsExcludes some details– With some plain English mixed inWith some plain English mixed in
![Page 15: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/15.jpg)
Some Core ConceptsSome Core Concepts• (A quick taste of what you'll be (A quick taste of what you'll be
learning in Python)learning in Python)
• StateState
• ExpressionsExpressions
• VariablesVariables
• ControlControl
![Page 16: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/16.jpg)
StateState
![Page 17: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/17.jpg)
ExpressionExpressionss
> print > print ““HelloHello””
HelloHello
> print (5+3)> print (5+3)88
>print (5*5-2)>print (5*5-2)2323
![Page 18: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/18.jpg)
VariablesVariables• Not exactly like AlgebraNot exactly like Algebra
• In Algebra x=5 (or 5=x) expresses a truthIn Algebra x=5 (or 5=x) expresses a truth
• In a program, x=5 updates something in the In a program, x=5 updates something in the computercomputer’’s memorys memory– (and 5=x wouldn(and 5=x wouldn’’t work: itt work: it’’s not a valid statement)s not a valid statement)
> x=0> print (x)0> x=5> print (x)5
![Page 19: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/19.jpg)
Control StructuresControl Structures
• Lets you make decisionsLets you make decisions
• for, while, …for, while, …
if(x>5): print (“x is big”)
Else: print (“x is small”)
![Page 20: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/20.jpg)
ProgramsPrograms
““The computer The computer programmer is a creator programmer is a creator of universes for which he of universes for which he alone is responsible. alone is responsible. Universes of virtually Universes of virtually unlimited complexity can unlimited complexity can be created in the form of be created in the form of computer programs.computer programs.””
![Page 21: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/21.jpg)
ComplexityComplexity
Controlling complexity is the Controlling complexity is the essence of computer essence of computer programmingprogramming
-Brian Kernigan -Brian Kernigan
![Page 22: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/22.jpg)
The ProcessThe Process
![Page 23: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/23.jpg)
But what doBut what do you you DO?DO?
• DesignDesign– RequirementsRequirements
• Use casesUse cases
– PseudocodePseudocode– Formal CodeFormal Code– TestTest– DebugDebug
![Page 24: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/24.jpg)
DesignDesign• Most programming is Most programming is
not done sitting in not done sitting in front of a computerfront of a computer
• You need to really You need to really understand what the understand what the program will doprogram will do
• How it interacts with How it interacts with the userthe user
• And how it is And how it is structured internallystructured internally
![Page 25: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/25.jpg)
Mostly, when you see programmers, they Mostly, when you see programmers, they aren't doing anything. One of the attractive aren't doing anything. One of the attractive things about programmers is that you things about programmers is that you cannot tell whether or not they are working cannot tell whether or not they are working simply by looking at them. Very often simply by looking at them. Very often they're sitting there seemingly drinking they're sitting there seemingly drinking coffee and gossiping, or just staring into coffee and gossiping, or just staring into space. What the programmer is trying to space. What the programmer is trying to do is get a handle on all the individual and do is get a handle on all the individual and unrelated ideas that are scampering unrelated ideas that are scampering around in his or her head.around in his or her head.
Charles M Strauss Charles M Strauss
![Page 26: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/26.jpg)
TestTest
• Testing a complicated program is difficultTesting a complicated program is difficult
• Some people write the tests before the Some people write the tests before the programsprograms
““Programming is like sex, one mistake and Programming is like sex, one mistake and you have to support it for the rest of your you have to support it for the rest of your lifelife..””
![Page 27: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/27.jpg)
DebugDebug• At the beginning, much of At the beginning, much of
your timeyour time
• In many ways more In many ways more challenging than codingchallenging than coding
• When you have When you have eliminated the eliminated the impossible, whatever impossible, whatever remains, remains, however however improbableimprobable, must be , must be the truth.the truth. – Sherlock – Sherlock HolmesHolmes
![Page 28: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/28.jpg)
•Millionaire!Millionaire!
![Page 29: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/29.jpg)
Beginning PythonBeginning Python
![Page 30: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/30.jpg)
What is Python?What is Python?
• A REAL Programming Language!A REAL Programming Language!• Developed in 1990 by Guido Van Rossum in Developed in 1990 by Guido Van Rossum in
the Netherlandsthe Netherlands• Named after Monte PythonNamed after Monte Python’’s Flying Circus!s Flying Circus!• Openly available at no charge!Openly available at no charge!• Freely distributed on the WebFreely distributed on the Web• LotLot’’s of Libraries of Python Routines availables of Libraries of Python Routines available• Increasingly popular for writing scripts for the Increasingly popular for writing scripts for the
WebWeb
![Page 31: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/31.jpg)
How does Python work?How does Python work?
• It is an INTERPRETED Language, (Like HTML It is an INTERPRETED Language, (Like HTML and Java) so that it is easy to write and and Java) so that it is easy to write and execute programs in an interactive modeexecute programs in an interactive mode
• It has a very clear and easy SyntaxIt has a very clear and easy Syntax
• It is portable so it runs on many different It is portable so it runs on many different Operating Systems and Machines (Windows, Operating Systems and Machines (Windows, MacOS, Linux, Unix, etc)MacOS, Linux, Unix, etc)
![Page 32: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/32.jpg)
What are we going to do What are we going to do with Python in CS2?with Python in CS2?• Learn some basic commands and Learn some basic commands and
capabilities of the languagecapabilities of the language– Printing data, evaluating expressions, Printing data, evaluating expressions,
inputting data, variables, loops, decisions, inputting data, variables, loops, decisions, functions, boolean expressionsfunctions, boolean expressions
• Learn how to use the programming Learn how to use the programming environmentenvironment
• Learn how to write simple but useful Learn how to write simple but useful programsprograms
![Page 33: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/33.jpg)
What tools will you require?What tools will you require?
• You will need the Python Interpreter and You will need the Python Interpreter and programming environmentprogramming environment
– Already installed on the computers in the LABAlready installed on the computers in the LAB– Can be downloaded and installed on your Can be downloaded and installed on your
personal machine from:personal machine from:•www.python.org•Hint…to be safe install the 32 bit version Hint…to be safe install the 32 bit version
for your OSfor your OS
• You will need the Python TutorialYou will need the Python Tutorial
![Page 34: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/34.jpg)
General ConventionsGeneral Conventions
• Python is Case Sensitive!…Be Careful!Python is Case Sensitive!…Be Careful!– Anything you name (variables, etc) must be Anything you name (variables, etc) must be
referred to exactly as initially typedreferred to exactly as initially typed
• Python Commands are always in lowercase!Python Commands are always in lowercase!
• Comments can be inserted anywhere by Comments can be inserted anywhere by using a using a ““## ”” before the comment text before the comment text
• Some commands require indentation to Some commands require indentation to work properlywork properly
![Page 35: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/35.jpg)
VariablesVariables
•You can think of variables as labeled jars that store different types of data. While there are several kinds of variables, today we're only going to look at two:
String VariablesNumber Variables
![Page 36: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/36.jpg)
String VariablesString Variables
• Strings are literal collections of text charactersStrings are literal collections of text characters
• Strings must be enclosed in either single or Strings must be enclosed in either single or double quotesdouble quotes
• Strings may be manipulated by a variety of Strings may be manipulated by a variety of operators (repetition, concatenation, etc)operators (repetition, concatenation, etc)
• Concatenation exampleConcatenation example– word = word = ““abcabc””– word = word + word = word + ““defdef””– print (word) print (word)
you will get: you will get: abcdefabcdef
![Page 37: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/37.jpg)
More String OperationsMore String Operations
• String RepetitionString Repetition– word = word = ““Yo! Yo! ””– print (word* 5)print (word* 5)
•Results in Results in Yo!Yo!Yo!Yo!Yo!Yo!Yo!Yo!Yo!Yo!
![Page 38: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/38.jpg)
String VariablesString Variables
• String - A string variable is a string of alphanumeric characters and allowed symbols that are contained within quotation marks. For example, "Hello world, I'm 102 years old today!" is an example of a string.
• Strings are basically used for storing text
• Example: greeting = “Good Morning Sunshine!”
![Page 39: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/39.jpg)
Number VariablesNumber Variables
• Number - A number variable couldn't be more straightforward because all number variables store are numbers. You don't store them within quotes like strings. Instead, numbers can just be written as they are. If you want to store the number 9 in a variable, you just write 9
• Example: my_bank_balance = 9Example: my_bank_balance = 9
or…. pi=3.14or…. pi=3.14
![Page 40: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/40.jpg)
Our First ProgramOur First Program
• A program that prints a greeting!A program that prints a greeting!
– print (print (““Hello, World!Hello, World!””))
• When this program runs, it outputs… When this program runs, it outputs… Hello, World!Hello, World!
• The text inside of the quotes in the program The text inside of the quotes in the program is referred to as a String, or String Variableis referred to as a String, or String Variable
![Page 41: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/41.jpg)
Another way to achieve the sameAnother way to achieve the sameresultresult
– greeting = greeting = ““Hello, World!Hello, World!””– print (greeting)print (greeting)
• The output is The output is Hello, World!Hello, World!
• Note that we assigned the variable Note that we assigned the variable name name greetinggreeting with the value of the with the value of the literal string inside of the quotes.literal string inside of the quotes.
![Page 42: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/42.jpg)
Expressions Expressions
• Another more complicated program:Another more complicated program:– print (print (““2 plus 2 is2 plus 2 is””, 2+2), 2+2)– print (print (““3 times 3 is3 times 3 is””, 3*3), 3*3)– print (print (““35 divided by 5 is35 divided by 5 is””, 35/5), 35/5)
• The output will be:The output will be:2 plus 2 is 42 plus 2 is 43 times 3 is 93 times 3 is 9
35 divided by 5 is 735 divided by 5 is 7
![Page 43: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/43.jpg)
Operations on NumbersOperations on Numbers
• OperationOperation Symbol Symbol ExampleExample
• ExponentiationExponentiation **** 5 ** 2 == 255 ** 2 == 25
• MultiplicationMultiplication ** 2 * 3 == 62 * 3 == 6
• DivisionDivision // 14 / 3 == 4 14 / 3 == 4 14 / 3.0 == 4.6666 14 / 3.0 == 4.6666
• RemainderRemainder %% 14 % 3 == 2 14 % 3 == 2
• AdditionAddition + 1 + 2 == 3+ 1 + 2 == 3
• Subtraction Subtraction -- 4 - 3 == 14 - 3 == 1
![Page 44: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/44.jpg)
More Variable Assignment ExamplesMore Variable Assignment Examples
• value = 4*8value = 4*8
• net_price =4.56net_price =4.56
• repeat_word = repeat_word = ““wordword”” * 5 * 5 • Variables are created and recognized dynamically, that is Variables are created and recognized dynamically, that is
when they appear in your program with a value assigned when they appear in your program with a value assigned to themto them
• Variables must be assigned before you refer to them Variables must be assigned before you refer to them otherwise an error will occurotherwise an error will occur
• The values that are assigned to variables in Python are The values that are assigned to variables in Python are called called Objects, each object having a reserved place in Objects, each object having a reserved place in memory with a pointer connecting it to the assigned memory with a pointer connecting it to the assigned variablevariable
![Page 45: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/45.jpg)
Variable manipulationVariable manipulation
• Variables can be used in conjunction with Variables can be used in conjunction with themselves to affect certain operations;themselves to affect certain operations;– Example:Example:
•speed=3speed=3
•speed=speed+4speed=speed+4
•speed=speed*3speed=speed*3
•print (speed) print (speed) WHAT is the output of this program? WHAT is the output of this program?
![Page 46: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/46.jpg)
Printing variablesPrinting variables
• Variables may be printed by themselves or in Variables may be printed by themselves or in successionsuccession
– ExampleExample
number = 9number = 9bignumber = 55bignumber = 55print (number)print (number)print (bignumber)print (bignumber)print (number , bignumber)print (number , bignumber)
(puts them on the same line)(puts them on the same line)
![Page 47: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/47.jpg)
Inputting Data into your Inputting Data into your programprogram• We will focus on We will focus on user inputuser input which is the simplest which is the simplest
type and suitable for our purposestype and suitable for our purposes
• Input command syntax is:Input command syntax is:
speed = input (speed = input (““enter speed enter speed ““)) OrOr name = raw_input (name = raw_input (““enter your name: enter your name: ““))
WhatWhat’’s the obvious difference between these input s the obvious difference between these input commands?commands?
![Page 48: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/48.jpg)
Ok, so letOk, so let’’s write a simple s write a simple program with the stuff weprogram with the stuff we’’ve ve covered so farcovered so far# A simple program to check our travel progress# A simple program to check our travel progress
name = raw_input ("enter your name: ") name = raw_input ("enter your name: ")
speed = input ("enter your average speed: ") speed = input ("enter your average speed: ")
time = input ("enter your exact travel time in hours: time = input ("enter your exact travel time in hours: ")")
print (name) + " your distance travelled is exactly print (name) + " your distance travelled is exactly " ,(speed*time), " miles”" ,(speed*time), " miles”
print "Travel safely!!! The End"print "Travel safely!!! The End"
![Page 49: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/49.jpg)
•Python DemoPython Demo
![Page 50: How to Think Like a Computer Programmer Beginning Python](https://reader035.vdocument.in/reader035/viewer/2022081506/5697c0221a28abf838cd3285/html5/thumbnails/50.jpg)
““That's what's cool about That's what's cool about working with computers. working with computers. They don't argue, they They don't argue, they remember everything and remember everything and they don't drink all your they don't drink all your beer.beer.””