Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
36 views
52 pages
Intriduction To Python Code in Place1
Uploaded by
PURUSHOTHAM V
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save intriduction_to_python_code_in_place1 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
36 views
52 pages
Intriduction To Python Code in Place1
Uploaded by
PURUSHOTHAM V
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save intriduction_to_python_code_in_place1 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save intriduction_to_python_code_in_place1 For Later
You are on page 1
/ 52
Search
Fullscreen
c ° x= — > a. 9° — c 2 —_ Y > oS ° oo =You had me at turn_left() Piech and Sahami, CS106A, Stanford UniversityFile: SteepleChasekarel.py Comments for program Karel runs a steeple chase that is 9 avenues long. i fles are of arbitrary height and placement. and every function To run a race that is 9 avenues long, we need to move ard or jump hurdles 8 times. def main(): for i in range(s): Decomposition principle: if front_is_clear(): Consistent a move() . * Each function should solve els. indentation one step of problem e: jump_hurdle() Pre-condition: Facing East at bottom of hurdle Post-condition: Facing East at bottom in next avenue after hurdle def cba Descriptive names nove()~ uraieQ Short functions (snake_case) descend_hurdle() (usually 1-15 lines)if mehran_teaching(): not_funny() while mehran_teaching(): not_funny()Guido van Rossum (Creator of Python) Monty Python’s Flying Circus KB Piech and Sahami, CS106A, Stanford UniversityToday’s Goal 1. Introduction to Python 2. Understanding variablesFill helloworld.py This is our first python program. It is customary to have a programmer's first program write “hello world" (inspired by the first program in Brian Kernighan and Dennis Ritchie's classic book, ‘The C Programming Language.') def main(): print("hello, world!") # This provided Line is required at the end of a Python # file to call the main() function. if __name__ _main_': main() # little bit different than in KarelDoweneaumm * ED aa © > C6 stamens seomteee Lectre se radutionto raven oars ©: sitenierscrm et main Prine (het, wort) mint)Bi ccrnmene-teow x | ° © 5 6 ttn Cerca ecto acitonto ries Fite: heltowor tip os oF main Prine(thette, works") “rein ramnevoy See ts) dowpssnes@| sarors 15 stBi ccrnmene-teow x | ° © 5 6 ttn Cerca ecto acitonto ries Fite: heltowor tip os oF main Prine(thette, works") “rein ramnevoy See ts) dowpssnes@| Grahars “16 python hettowort4.9y stBi ccrnmene-teow x | a © 5 6 ttn Cerca oF main Prine(thette, works") “rein ramnevoy See ts) dowpssnes@| Grakars “16 python het toward. pyYou’re now all Python programmers! hey_that_looks_ like_what_I_ taught_them()def main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers int(num1) input("Enter second number: ") int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers nter first number: ") num1 = int(num1) num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers nter first number: ") num1 = int(num1) num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: 9 Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") input("Enter second number: ") num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: 9 Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1 num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: 9 Enter second number: Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1 num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: 9 Enter second number: 17 Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 nter_second _numbe! num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: 9 Enter second number: 17 Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 = input("Enter second number: ") "+ str(total) + ".") This program adds two numbers. Enter first number: 9 Enter second number: 17 Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 This program adds two numbers. Enter first number: 9 Enter second number: 17 The total is 26. Piech and Sahami, CS106A, Stanford Universityprint("This program adds two numbers.") * print command prints text to the terminal * Text printed is between double quotes ("text") — Can also be between single quotes ('text') — Choice of quotes depends on text you are printing * Double quotes when text contains single quotes print("no, you didn't") ® no, you didn't * Single quotes when text contains double quotes print('say "hi" Karel') ® say "hi" Kar Piech and Sahami, CS106A, Stanford Universitynum1 = input(“Enter first number: ") input command gets text input from the user Prints text specified in double/single quotes — Then waits for user input — Here, user input from input is put in a variable (num1) — The user input is considered text, even if user entered a number We'll talk more about input function later Piech and Sahami, CS106A, Stanford University* Avariable is a place to store information in a program * It associates a name with a value * You can create a new variable by assigning a value: x = 10 Piech and Sahami, CS106A, Stanford University_ Whatisa Variable? *Ls | * Avariable is a place to store information in a program * It associates a name with a value * You can create a new variable by assigning a value: x = 10 — The value can change with a new assignment x=5 Piech and Sahami, CS106A, Stanford University_ Whatisa Variable? xL2 | * Avariable is a place to store information in a program * It associates a name with a value * You can create a new variable by assigning a value: x = 10 — The value can change with a new assignment x=5 — You can set the value using mathematical expressions x=5 +7 — More about expressions next class Piech and Sahami, CS106A, Stanford University* You use the equal sign (=) to assign to a variable — The first time you assign a value to a variable, you create it — Subsequent assignments give the variable a new value * Assignment is not the same as "equals" in math — Assignment: first evaluate right-hand side, then assign to the variable on the left-hand side — Consider the following code: total = 5 total = total + 1 * Variables are only visible inside the function in which they are created (called "scope" of variable) — If you create a variable in main ( ), its only visible in main () — More on that next class Piech and Sahami, CS106A, Stanford University* Variable names must: — Start with a letter or an underscore (_ ) — Contain only letters, digits, or underscores — Cannot be a "built in" command in Python (e.g., for) * Variable names are case sensitive — Hello is not the name as hello * Variable names should: — Be descriptive of the value they refer to * E.g., x is only a good name if it's a coordinate — Be in snake case (e.g., num_students) Piech and Sahami, CS106A, Stanford University* When you store information in a variable, it becomes a Python object — Objects come in different sizes and types * Think about a Python object as a suitcase stored in your computer’s memory — Object take up different amounts of RAM depending on what you’re storing. You have space for wnillions on suitcases! Piech and Sahami, CS106A, Stanford UniversitySuitcase Analogy * Variable is a luggage tag that gives a name to suitcase num_students = 700 — Value is what is stored in the suitcase — Create the tag/suitcase the first time you assign to variable [=i Sf num_in_class = 550 Python handles the —— baggage for you! [ sins SD num_absent = num_students — num_in_ class [sew SED* Each suitcase knows what type of information it carries num_students = 700 — Value stored in suitcase is an integer (called an int in Python) — Suitcase keeps track of type of data that is stored there num_students = 700.0 # note decimal point — Now, value stored is a real number (called a float in Python) — heen Piech and Sahami, CS106A, Stanford University c=* int: integer value (no decimal point) x = 10 you -2 * float: real number value (has decimal point) x = 5.0 y = -3.7 * string: text characters (between single/double quotes) x = "hello" y = '10' — Note: the string "5" is not the same as the integer 5 * bool: Boolean logical values (True/False) x = True y = False * More on strings and bools in a few days Piech and Sahami, CS106A, Stanford University* How much do | weigh? fel — Answer can be a real valued number — There is no "next" number ‘ee — This would be a float * How many children do | have? — Answer is an integer — There is a well-defined "next" number — This would be an int Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") Po Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. * print command is displaying a string Piech and Sahami, CS106A, Stanford Universitydef main(): i “This program adds two numbers.” num1 = input("Enter first number: ") num1 = int(num1) num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: 9 * input command gives you back a string — Even if the user types in a number Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input(“Enter first number: num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") va] This program adds two numbers. Enter first number: 9 * Create int version of string and assign it back to n Piech and Sahami, CS106A, Stanford UniversityShow Me The Luggage! * input command gives you back a string num1 = input("Enter first number: ") — We create an integer version of num1 — Create a new suitcase that has int version of num1 — Then assign the tag num1 to that piece of luggage nt(num1) ‘= o—{Pdef main(): print("This program adds two numbers.") num1 = input(“Enter first number: num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") va] This program adds two numbers. Enter first number: 9 * Create int version of string and assign it back to n Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1 num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: 9 Enter second number: Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1 num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: 9 Enter second number: 17 Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 nter_second _numbe! num2 = int(num2) total = num1 + num2 print("The total is " + str(total) + ".") This program adds two numbers. Enter first number: 9 Enter second number: 17 Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 = input("Enter second number: ") "+ str(total) + ".") This program adds two numbers. Enter first number: 9 Enter second number: 17 Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 This program adds two numbers. Enter first number: 9 Enter second number: 17 The total is 26. Piech and Sahami, CS106A, Stanford University* Adding strings in print command?! print("The total is " + str(total) + ".") * The + operator concatenates strings together strl = "hi" str2 =" " str3 = "there" str4 = strl + str2 + str3 * total is integer, so we need to create a string version str(total) — String version of total is a new value that is concatenated to produce final string that is printed — Original variable total is still an int Piech and Sahami, CS106A, Stanford Universitydef main(): print("This program adds two numbers.") num1 = input("Enter first number: ") num1 = int(num1) num2 = input("Enter second number: ") num2 = int(num2) total = num1 + num2 This program adds two numbers. Enter first number: 9 Enter second number: 17 The total is 26. Piech and Sahami, CS106A, Stanford University* You can print numbers by themselves directly — Only need to create string version of numbers when printing other text (strings) with them def main(): x = 10 y = 3.5 print(x) print(y) print("x = " + str(x)) ww ro 10 Piech and Sahami, CS106A, Stanford University* You can also print multiple items separating them with commas — By default, a space is printed between each item def main(): x=4 y = 0.2 print(x, y) print("x =", x, “and y =", y) Piech and Sahami, CS106A, Stanford UniversityYou just wrote your first Python program and learned about variables!Today’s Goal 1. Introduction to Python 2. Understanding variablesadd2numbers.py
You might also like
4 IntroPython
PDF
No ratings yet
4 IntroPython
52 pages
W3 Python Lecture Slides
PDF
No ratings yet
W3 Python Lecture Slides
52 pages
Introduction To Python: Chris Piech and Mehran Sahami CS106A, Stanford University
PDF
No ratings yet
Introduction To Python: Chris Piech and Mehran Sahami CS106A, Stanford University
50 pages
Python Jan 2025
PDF
No ratings yet
Python Jan 2025
23 pages
1 Introduction To The Syntax of Python. Part I: Numerical
PDF
No ratings yet
1 Introduction To The Syntax of Python. Part I: Numerical
10 pages
Paython
PDF
No ratings yet
Paython
66 pages
Batangas State University
PDF
No ratings yet
Batangas State University
54 pages
Lab 2 - Operators
PDF
No ratings yet
Lab 2 - Operators
10 pages
Py Basic
PDF
No ratings yet
Py Basic
59 pages
Introduction To Python
PDF
No ratings yet
Introduction To Python
27 pages
01 Python Variables Types and Basic Io
PDF
No ratings yet
01 Python Variables Types and Basic Io
8 pages
Python Handout 2
PDF
No ratings yet
Python Handout 2
6 pages
Pdfjoiner
PDF
No ratings yet
Pdfjoiner
13 pages
Python Notes
PDF
No ratings yet
Python Notes
8 pages
A2018003884 23318 16 2025 Python
PDF
No ratings yet
A2018003884 23318 16 2025 Python
66 pages
Python Programming Notes
PDF
No ratings yet
Python Programming Notes
62 pages
Lectures Data Processing
PDF
No ratings yet
Lectures Data Processing
7 pages
Unit 1 Python
PDF
No ratings yet
Unit 1 Python
248 pages
P01 Introduction To Python Ans
PDF
No ratings yet
P01 Introduction To Python Ans
30 pages
TalkFile - Chapter02. Strings, Variables, Input & Output Functions PDF
PDF
No ratings yet
TalkFile - Chapter02. Strings, Variables, Input & Output Functions PDF
21 pages
Pychapt2 New
PDF
No ratings yet
Pychapt2 New
44 pages
ELE492 - Lecture 2 - 28-02-2023
PDF
No ratings yet
ELE492 - Lecture 2 - 28-02-2023
99 pages
CHAPTER 1 - Introduction-F - Part 1
PDF
No ratings yet
CHAPTER 1 - Introduction-F - Part 1
23 pages
Part-I Basic Python Programming
PDF
No ratings yet
Part-I Basic Python Programming
40 pages
Lecture 2
PDF
No ratings yet
Lecture 2
31 pages
Python Notes
PDF
No ratings yet
Python Notes
18 pages
Introduction To Python Programming
PDF
No ratings yet
Introduction To Python Programming
55 pages
Python - Prep Work
PDF
No ratings yet
Python - Prep Work
129 pages
Stored Programs: Mastering Cyberspace: An Introduction To Practical Computing
PDF
No ratings yet
Stored Programs: Mastering Cyberspace: An Introduction To Practical Computing
9 pages
Programming
PDF
No ratings yet
Programming
43 pages
Python
PDF
No ratings yet
Python
91 pages
TECH1200 Week 1 Workshop
PDF
No ratings yet
TECH1200 Week 1 Workshop
29 pages
Mct-242: Computer Programming-I: Using Python 3.9
PDF
No ratings yet
Mct-242: Computer Programming-I: Using Python 3.9
10 pages
P YTHON
PDF
No ratings yet
P YTHON
32 pages
Tutorial2DataandExpression1 95176
PDF
No ratings yet
Tutorial2DataandExpression1 95176
40 pages
Introduction To Python
PDF
No ratings yet
Introduction To Python
56 pages
Python Lecture For Beginners
PDF
No ratings yet
Python Lecture For Beginners
45 pages
Python-Basic-Elements
PDF
No ratings yet
Python-Basic-Elements
111 pages
Python Unit 1 Chapter 1
PDF
No ratings yet
Python Unit 1 Chapter 1
63 pages
AI Python Lab1
PDF
No ratings yet
AI Python Lab1
13 pages
Chap 1
PDF
No ratings yet
Chap 1
65 pages
Ch02 Expressions
PDF
No ratings yet
Ch02 Expressions
36 pages
Basic Programming: CS 111: Computer Science For Scientists
PDF
No ratings yet
Basic Programming: CS 111: Computer Science For Scientists
47 pages
Phython 1-3
PDF
No ratings yet
Phython 1-3
6 pages
Comp Paper 2
PDF
No ratings yet
Comp Paper 2
241 pages
Comp Paper 2
PDF
No ratings yet
Comp Paper 2
211 pages
Table of Contents: Plan Practice
PDF
No ratings yet
Table of Contents: Plan Practice
61 pages
1 Python
PDF
No ratings yet
1 Python
38 pages
Lec02 PythonIntro - T
PDF
No ratings yet
Lec02 PythonIntro - T
40 pages
Variables and Data Types
PDF
No ratings yet
Variables and Data Types
13 pages
04 Python 1
PDF
No ratings yet
04 Python 1
43 pages
Lab Exercises Solutions
PDF
No ratings yet
Lab Exercises Solutions
16 pages
Introduction To Python
PDF
No ratings yet
Introduction To Python
20 pages
PFE 1021 TH Week1 N
PDF
No ratings yet
PFE 1021 TH Week1 N
30 pages
Introduction To Python
PDF
No ratings yet
Introduction To Python
36 pages
06 Python Tutorial
PDF
No ratings yet
06 Python Tutorial
16 pages
Python
PDF
No ratings yet
Python
38 pages
ECE PE5: Instrumentation & Control Track "Computer Vision System With Applied Robotics"
PDF
No ratings yet
ECE PE5: Instrumentation & Control Track "Computer Vision System With Applied Robotics"
45 pages
Power Automation
PDF
No ratings yet
Power Automation
4 pages
Iics
PDF
No ratings yet
Iics
5 pages
Daily Activity Tracking
PDF
No ratings yet
Daily Activity Tracking
7 pages
Python Practice
PDF
No ratings yet
Python Practice
59 pages
Python Certificate Purushohtam Code in Place
PDF
No ratings yet
Python Certificate Purushohtam Code in Place
1 page