Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

Similar documents
MITOCW watch?v=0jljzrnhwoi

1 Getting used to Python

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro. Scheme Basics. scm> 5 5. scm>

This lesson is part 5 of 5 in a series. You can go to Invoice, Part 1: Free Shipping if you'd like to start from the beginning.

Decisions, Decisions. Testing, testing C H A P T E R 7

MITOCW watch?v=9h6muyzjms0

Post Experiment Interview Questions

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

MITOCW watch?v=kz7jjltq9r4

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

MITOCW watch?v=zm5mw5nkzjg

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

MITOCW watch?v=se4p7ivcune

Linked Lists. What is a Linked List?

The following content is provided under a Creative Commons license. Your support

Azon Master Class. By Ryan Stevenson Guidebook #5 WordPress Usage

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

What's the Slope of a Line?

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Skill 1: Multiplying Polynomials

1 Strings (Review) CS151: Problem Solving and Programming

MITOCW watch?v=w_-sx4vr53m

Lesson 4: Who Goes There?

Watch the video below to learn more about number formats in Excel. *Video removed from printing pages. Why use number formats?

These are notes for the third lecture; if statements and loops.

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

MITOCW watch?v=zlohv4xq_ti

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Text Input and Conditionals

The following content is provided under a Creative Commons license. Your support

Learning to Program with Haiku

Python for Informatics

The following content is provided under a Creative Commons license. Your support

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Intro. Classes & Inheritance

MITOCW watch?v=flgjisf3l78

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

Module 6. Campaign Layering

T H E I N T E R A C T I V E S H E L L

Basics of Programming with Python

cs1114 REVIEW of details test closed laptop period

Intro. Comparisons. > x > y if and only if x is bigger than y. < x < y if and only if x is smaller than y.

CS 11 python track: lecture 3. n Today: Useful coding idioms

MITOCW watch?v=ytpjdnlu9ug

The Stack, Free Store, and Global Namespace

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Java Programming Constructs Java Programming 2 Lesson 1

Part II Composition of Functions

Hardware versus software

Intro. Speed V Growth

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Comp 151. Control structures.

The following content is provided under a Creative Commons license. Your support

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Flow Control. So Far: Writing simple statements that get executed one after another.

Smart formatting for better compatibility between OpenOffice.org and Microsoft Office

CSC 108H: Introduction to Computer Programming. Summer Marek Janicki

CS 106 Introduction to Computer Science I

Java Programming. Computer Science 112

MITOCW MIT6_01SC_rec2_300k.mp4

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Flow Control: Branches and loops

CS1 Lecture 5 Jan. 25, 2019

Lesson 3 Transcript: Part 2 of 2 Tools & Scripting

Fortunately, you only need to know 10% of what's in the main page to get 90% of the benefit. This page will show you that 10%.

Lab 2: Conservation of Momentum

PROFESSOR: Well, yesterday we learned a bit about symbolic manipulation, and we wrote a rather stylized

Quiz 3; Tuesday, January 27; 5 minutes; 5 points [Solutions follow on next page]

Clickteam Fusion 2.5 Creating a Debug System - Guide

Troubleshooting Maple Worksheets: Common Problems

printf( Please enter another number: ); scanf( %d, &num2);

CS103 Spring 2018 Mathematical Vocabulary

Programming with Python

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

The following content is provided under a Creative Commons license. Your support

Decision Structures CSC1310. Python Programming, 2/e 1

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

MITOCW ocw apr k

CSE143 Notes for Monday, 4/25/11

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

--APOPHYSIS INSTALLATION AND BASIC USE TUTORIAL--

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

BEGINNER PHP Table of Contents

Azon Master Class. By Ryan Stevenson Guidebook #11 Squidoo Marketing

MITOCW ocw f99-lec07_300k

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

MITOCW ocw f99-lec12_300k

How to Create a Killer Resources Page (That's Crazy Profitable)

MITOCW watch?v=4dj1oguwtem

Mathematical Logic Part One

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

USING DRUPAL. Hampshire College Website Editors Guide

Chrome if I want to. What that should do, is have my specifications run against four different instances of Chrome, in parallel.

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Functions and Decomposition

Transcription:

With notes! 1

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few useful functions (some built into standard Python; some requiring you to import a "module" first) Finally, I'll talk about commenting your code - a very simple but important topic 2

3

4

5

Control statements are things like the if statement here, which control the flow of your program. Each control statement has some sort of condition which it checks for True-ness or False-ness. For example, here the condition is simply the variable "construction". Since the value of this condition is False, we do not execute the "if" block, and instead execute the "else" block. Thus, we have changed the flow of the program based on the value of the variable "construction". Control statements also include loops, which we'll talk about today. 6

8

The conditional can be pretty much anything that can be evaluated as either True or False. See next slide. 9

10

Most of these are pretty intuitive. The big one people tend to mess up on in the beginning is ==. Remember, a single equals sign means assignment. A double equals means is the same as/is equal to. You will NEVER use a single equals sign in a conditional statement. 11

https://goo.gl/forms/x1g57mv2qxj8txqg2

Since the line 'print "Goodbye now!" ' is not indented, it is NOT considered part of the if statement. Therefore, it is printed no matter how the if-statement is evaluated.

Since a and b are not both True, we go to the else statement.

By using "not" before b, we negate its current value (False), making b True. Thus the entire conditional as a whole becomes True, and we execute the if-block.

"not" only applies to what's directly in front of it. So a more clear way to show this statement might be like this: (not a) and b This turns into (False) and False and thus the overall conditional is not true. We therefore go to the else statement.

Using parentheses works like you'd expect in conditionals: grouped statements are evaluated first. So Python first decides: (a and b)? ("are a and b both true?") -> False Then it applies the not, which flips the False into a True. So then the final answer is True. Here's another way of breaking down what's happening, in order: not (a and b) not (True and False) not (False) True

"is a or b True?" As you'd expect, with "or" only one of them has to be True

How to solve these? Sometimes it's best to just write it down, and write down True or False as you solve each part. ((a == 1) and (b > 0)) or (b == (2 * a)) ((5 == 1) and (10 > 0)) or (10 == (2 * 5)) ((False) and (True)) or (True) (False) or (True) True Hopefully you won't ever have to write or understand such a convoluted conditional. But, if you do, make sure to logic-check it very thoroughly, because it's very easy to make mistakes.

Some people recommend using 4 spaces instead of a tab. I believe this is the official Python recommendation as well. Personally, I prefer tabs. People argue about this a lot, and there's no real consensus, unfortunately. You can pretty much use whatever you want when you code on your own, but if you ever start collaborating with someone else on the same code, just make sure you agree on one convention, or you're in for a real headache. 36

Use "if" for the first block, "elif" for the middle blocks, and "else" for the final block (again, else is optional--it's like a catch-all for anything that didn't match an earlier statement) 37

38

39

"Built-in" is in contrast the the non-built-in functions, which are packaged into modules of similar functions (e.g. "math") that you must import before using. More on this in a minute! You can also create your own functions, which is super useful...more on that in a future class! We'll talk more about what a function actually IS when we get to that class. For now, it's fine to think of them like commands. 40

41

What happens if you don't assign the result of raw_input() to some variable? > Well, nothing. You lose whatever was read. When you use a function, you only have one chance to "capture" their output. However, sometimes we don't care about the output, and in those cases we can just ignore it. 42

Side note: you actually won't use raw_input() very much, if ever, after this lesson. There are much better ways of obtaining dynamic input e.g. command line arguments and reading from files. We're only going over it because it's the only way of getting outside input that doesn't require explaining a whole lot of other concepts. 43

44

45

46

47

48

49

Replace <modulename> with the name of the module you want, and <functionname> with the name of a function in the module. The <modulename>.<functionname> synatx is needed so that Python knows where the function comes from. There can potentially be other functions with the same name (e.g. 50

51

random.random() - Return the next random floating point number in the range [0.0, 1.0). random.randint(a, b) - Return a random integer N such that a <= N <= b. random.gauss(mu, sigma) - draw from the Normal distribution. mu is the mean, and sigma is the standard deviation. 52

53

54

55

PyDocs: the documentation of all Python, found here: https://docs.python.org/2/index.html This will often pop up as the first google search hit, so it's important to be able to understand the various conventions used 56

57

58

59

60

Everything after the # sign on the same line will be ignored. 61

Everything between the pairs of """ or ''' will be ignored Same thing about indenting goes for these -- they must have the same indent as the code that surrounds them 62

In general, you should strive to write code that is so clear that it doesn't need comments. This isn't always practical, though... 63

64