Class code for pythonroom.com cchsp2cs

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists Introduction to Computing Science and Programming I.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Computer Science 101 Introduction to Programming.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
Python Programming Fundamentals
Lists in Python.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Computer Science 101 Introduction to Programming.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Controlling Program Flow with Decision Structures.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
1 2. Program Construction in Java. 01 Java basics.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
G. Pullaiah College of Engineering and Technology
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Introduction to Python
Topic: Python’s building blocks -> Statements
Tuples and Lists.
Programming for Mobile Technologies
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
Variables, Expressions, and IO
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Functions CIS 40 – Introduction to Programming in Python
Arrays, For loop While loop Do while loop
Introduction to Strings
Introduction to Strings
Arithmetic operations, decisions and looping
Lists in Python.
Variables ICS2O.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Place Value, Names for Numbers, and Reading Tables
Learning Outcomes –Lesson 4
Numbers.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
PHP.
CSI 101 Elements of Computing Spring 2009
String and Lists Dr. José M. Reyes Álamo.
Introduction to Programming Using Python PART 2
More elements of Python programs
Chapter 3 Operators and Expressions
Data Types and Expressions
For loops Taken from notes by Dr. Neil Moore
Introduction to Computer Science
COMPUTER PROGRAMMING SKILLS
Primitive Types and Expressions
Introduction to Strings
Python Review
Chapter 2 Variables.
Comparing Python and Java
Using C++ Arithmetic Operators and Control Structures
Introduction to Strings
Presentation transcript:

Class code for pythonroom.com cchsp2cs

Python basics Presentation Name Course Name Unit # – Lesson #.# – Lesson Name This presentation will cover the basics of Python. Python basics

Canopy environment All languages use a development environment. We will use Python in the Canopy development environment. It has two main parts. You can edit and run whole programs in the code editor. They don't run when you type; they only run when you execute them with this green arrow. You can execute a single line of code in the interactive session. You can expand either section by dragging the line between them.

Variables and Arithmetic In []: a = 3   In []: a * 7 Out[]: 21 In []: a ** 2 Out[]: 9 Working in the interactive session, You can do some arithmetic. Here we store the number 3 in the variable "a". Then we evaluate two expressions. a times 7, that's 3 times 7: 21. a to the power of 2, that's 3 squared: 9.

Expression in mathematics Expressions Expression in mathematics Expression in Python 2 + 3 2+3 2 - 3 2-3 2×3 = 2•3 = 2(3) 2*3 The operators for arithmetic are a little different than mathematics. We never use the asterisk in math. In math, we show multiplication using the times sign, a dot at the center of the line, or by writing two things next to each other, sometimes using parentheses to show that the two numbers are separate factors.

Data Types Data type Examples int 3 float 3. 3.4 str '3' bool False list [False, '3', 3.14] Every value in Python has a type. Numbers can be int or float types. Floats have a decimal point in them. Data stored in int can only be an integer, while floats can be any number, including the integers. (Integers are 0, 1,2,3,… and the negative integers -1,-2,-3,…)

Expressions for enrichment Expression in mathematics Expression in Python 2 3 or ⅔ 2/3 2 3 or ⅔ 2./3 23 2**3 3 = 31/2 = 30.5 3**(1./2)or 3**(0.5) Dividing two integer types rounds down so that the result is also an int. This is the "floor function" or "greatest integer function" shown on the left. Use a float when dividing if you want a float- that's what the decimal point next to the 2 does; it make the 2 a float, so the result will be a float: 0.6666666. Python uses ** for exponents. Most programming languages use ^. Enrichment: Powers can be fractions. The denominator takes the root; one half takes a square root, one third takes a cube root, and so on. You can use a decimal equivalent for the fraction, too.

Lists, Elements by Index In []: a = [10, 12, 13] In []: a[0] Out[]: 10 In []: a[1] Out[]: 12 One type of data is a list. Each element of the list can be any of the type of data. You can even have a list that contains a list. This list contains three ints: 10, 12, and 13. We use the square brackets to write a list and separate the elements with a comma. We also use square brackets alongside a variable name. App Inventor is one-indexed: the first element in the list has index 1. Python is zero-indexed. The first element has index 0. To read this aloud, say "a index zero or "a zero." It's value is 10. The value of the element at index 1, a[1], is 12.

Iteration for n in [1, 2, 3]: print n**2 Output: 1 4 9 You just learned about lists. Lists are useful for iteration. The "for" loop in Python executes over and over. The "for" block, shown by indentation, starts at the colon and includes all the lines of code indented after the "for" statement. The "for" block is executed once for each value in the list. Output: 1 4 9

Built in functions: In []: abs(4) Out[]: 4 In []: abs(-4) In []: range(7) Out[]: [0, 1, 2, 3, 4, 5, 6] In []: len(['a', 2, 4e12]) Out[]: 3 Python has 30 keywords like "for", "in", "print", and so on. Python also has 80 built-in functions. Functions are "called," using parentheses. Arguments go inside the parentheses. Many of the Python built-in functions are math functions, like the absolute value function shown here. The range function produces a list, starting at 0, counting by 1s, and STOPPING SHORT of the argument. That way range(7) is a list of seven numbers, from 0 to 6, stopping short of 7. The len() function tells you the length of a list— the number of items in the list.

Counted loop for n in range(3): print n Output: 1 2 1 2 You can create a counted loop using the range() function. Whatever number you put in the range function, the "for" block will get executed that many times.

for n in range(3): print n*2 Counted loop Don’t assign to the loop index You can evaluate the loop index for n in range(3): print n*2 Output: 2 4 n here is called the loop index. You can use the value of the loop index, as we do here, evaluating n squared. But don't assign to the loop index. It would not be ok, for example, to make n = n+1 inside the loop. Although the assignment statement and loop would execute in a predictable manner, it is hard to read and can be a source of hard-to-find bugs. Don't assign to a loop index.

Optional enrichment: for n in 'this': print n*2, Output: tthhiiss Iteration across strings Multiplication with characters Comma for no-new-line print for n in 'this': print n*2, Output: tthhiiss

Conditionals a = 2 if a == 2: print a else: print a+1 print a+2 Indentation is also used after a colon to show which code should get executed when a conditional is true or false. Output: 2 4

Debugging: When you get an error, look at the last line!