# If Statements, For Loops, Functions

Size: px
Start display at page:

Transcription

1 Fundamentals of Programming If Statements, For Loops, Functions Table of Contents Hello World Types of Variables Integers and Floats String Boolean Relational Operators Lists Conditionals If and Else Statements The Elif Statement Loops Functions Conclusion In this module, we will be reviewing three of the most critical fundamental concepts of programming in the Python language: types of conditionals, loops, and functions. Please check out the in-browser IDE (fancy word for a place that runs Python code) provided to you so that you can experiment and write code as we go through this document. For those unfamiliar with the language, don t worry! This first module will introduce you to the Python programming language and help you get comfortable with it. Now, let s start off by saying Hello to the world in our program!

2 Hello World In programming languages we can input commands that the machine will execute. For example, if we want to say Hello World in Python, we would write: print "Hello World" Then press the Test Run button and see below: Here, my program tells the machine to print the sentence "Hello World," which it then prints in grey text. We could also solve arithmetic problems and print out answers. For example, we can write code to see that 2 * 2 = 4. We can also solve more complex problems and see the result. For example, what is 2 10? We can see the result of this by typing print 2**10, another way of saying 2 10.

4 Types of Variables This will be a more tedious section, where we ll go over some vocabulary and make some distinct points that will help guide us through the rest of this document. While we ll often work with numbers, there s also a strong need for us to work with words and other forms of information, which we will refer to as data types. One example of such a data type we ve already seen is the String, which we saw in the form of "Hello World!" Other data types that computer scientists work with include the boolean and the list. Integers and Floats When dealing with numbers, we can break them down into two basic variables types: (there are actually a large variety of them but we only need to worry about 2 of them). Integer - A whole number that can be positive or negative (including zero) Examples: 0, 2, 1337, 50012, Float - A decimal number Examples: 1.324, 1.0, 0.0, It s usually enough to refer to these two data types as numbers. We will rarely need to worry about the difference between these two types when working with Python. String The String is a fancy way of saying that we re dealing with a sequence of letters, numbers, and other special characters (e.g. \$, %, #,). If you want to see what is accepted as a character check out the ascii table of characters. As a word of warning though, this table will look very complicated. The only column you need to concern yourself with is the char table for now. To write a String in Python, we wrap it with either single quotations or double quotations. Here are some examples below. "September" "Blarg, this sentencedoesn t;;makesense!!!" 'This is a single quotation String' """This is a multi line String! It can take up as many lines as it wants because I used three quotations!""" Here are examples of what will not be Strings. "This is not a complete String because I started the String with a double quotation and ended with a single quotation, so it never finished' ""I did two quotations, so I made a String with nothing beforehand and with nothing after""

5 Boolean The Boolean can be closely compared to a light switch. Like how the average lightswitch will have two states - an on and off state - the Boolean has two states. This binary decision is usually expressed with a True and a False, True being 1 and False being 0. In this section I will not be going into the technicalities of the Boolean, so it will be enough to think of the boolean as being either True or False. Below I will share some examples of how we might write booleans. x = True #Set the variable x equal to the True value x = False #Set x equal to False value Keep in mind though that the Boolean differs in that it is a statement: a matter of fact. We can ask questions in real life and see what the results are: Is x equal to 5? True, it is! We can do the same in programming to get information that helps us function. To check if x is equal to 5, we would go ahead and write this: x = 5 #Set the variable x equal to 5 print x == 5 Here, we re using something called a relational operator (More on this in the next section) to establish the relationship between the right hand side and the left hand side of the statement. Is x equal to 5? In this case, we would see that the result is true. Here are more examples that demonstrate how we might use relational operators (specifically is equal to) to interpret information. y = 5 x = (y == 5) print x Here I check if y is equal to 5. To check equality, we use ==. Because y is equal to 5, I have set x = True in a roundabout way. y = 4 x = (y == 5) print x Check if y is equal to 5. Because y is not equal to 5, I have set x = False here. I think print the value to see what it turns out being. Relational Operators Relational Operators allow us to effectively turn our statements into simple Boolean values that we can use. We ve already seen a very common operator, which checks equality. The == operator will check whether the left value holds equal value to the right value, and will return True or False depending on the result of the comparison. Here is a list of some example operators that we can use and what it means.

6 == #Checks Equality < #Check if the left value is less than the right value <= #Check if the left value is less than or equal to the right value > #Check if the left value is greater than the right value >= #Check if left value is greater than or equal to right value!= #Check if the two values are not equal to each other Booleans can take a lot of time to get used to. I would recommend going to your programming playground within Udacity and giving things a try and seeing what happens! Lists When dealing with a lot of information, there s a strong need for us to be able to store a lot of variables easily. For example, maybe we re doing a little statistics and getting the mean of a list of numbers. The List helps us organize and store a seemingly limitless amount of information into a single container. We can represent a list by placing values inside of brackets like such: my_list = [1, 2, 3, 4, 5] string_list = ["apple", "orange", "banana"] When creating a list, we can imagine that we ve created a series of compartments like we would see at the post office. We can then insert elements into each compartment, take a look at those elements, or replace elements at will. We can then just as easily pull the elements out for analysis. We do so as shown below. As we can see, we started off with a list that contains three elements. I started off by printing the list as a whole, so I got back my apple, orange, and banana. Afterwards, I checked what is in the first element of my list (more on this later). I then chose to go to my second element, my_list[1] which has "orange", and then replaced that value with "mango". Finally, I went ahead and printed out the second

7 element to see the change I made and then I printed out the list as a whole to see what happened there. At this point, what should stand out to you is how we referenced elements in our list. When we wanted to get the first element in our list, we did my_list[0]. Oddly, we stated that our first element is at position 0 while the second element is at position 1. This is a critical detail in Computer Science that is important to remember. In general, computer scientists like to index starting from 0. So when we reference position 1, it s actually going to be 0 for us. So practically, it ll be a lot easier if we reference "apple" as being our 0th element in our list. You can see a somewhat technical discussion on the topic here. This blogpost also provides an interesting perspective. Lists also have special properties that we can take advantage of. If we want to determine the length of a list, then we can do so with the len() command. Here s an example of how we would use it: len(my_list) We can also add elements, or lockers, to our already made list. To do so, we ll simply append. Here is an example of what our line of code will look like: my_list.append("pineapple") After this command, we ll find that the element pineapple will be added to the end of our list, so the length of our list will be one longer as well.

8 Conditionals We ll quickly find that things can feel very constrained using only simple statements. Imagine if we re given a problem that requires us to turn a random number that ranges from 1-12 into the appropriate month. When I refer to this problem, it will be marked as the month interpreter problem. There are many ways in which we can solve this problem. I ll go ahead and share one such solution that we can do using only Lists. So here, I went ahead and created a list containing all of my months. I then proceed to print out the appropriate month based on the value of x. Note though that when I pull the element from my list, my index is actually x - 1, not x. This is because our index starts at 0 instead of 1, so we actually have our months lined at 0-11 instead of By subtracting 1, I correct the off by one error and am able to easily print out my month. Let s say we couldn t use this solution though. How would we go about solving this problem? Ideally, we d like to say: If x is 1, then print January. If x is 2, then print February if x is 12, then print December.

9 If and Else Statements In Python, we can represent this logic with if statements. Let s start things off by first writing some code to check if we should print out January for an arbitrary value of x. See the code below. Here we re printing out "January" and not "Not January" because we set the value of x to be 1. We controlled our output based on a condition. Let s take a close look at the diagram below to get a better idea of how this program flows and operators. This diagram helps us map out the logic flow of a basic if statement. We have appropriate code written before the if statement. We ll then enter our if condition. The syntax for doing so is: if <Boolean>: where <Boolean> represents the Boolean value that I will insert there. Afterwards, depending on the result of the Boolean value, we ll execute different commands. If the boolean value is True,we ll execute the Then Body. If boolean value is False, then we ll execute the else Body. Going back to our if statement, we see that we would print "January" if our x is equal to 1, but otherwise we will always print out "Not January" because our Boolean will be False. Let s take this concept and apply it to our problem at hand now. Once we do, our code will look something like this:

10 Now we have effective code that will run and print the month based on our variable, x. We ll go through each if statement and then determine the month we should print out, but let s say that we want to provide some feedback. What if x is not a number between 1 and 12? Well, my first instinct was to add an else statement. Let s see what happens when I add the else statement though.

11 Although the value of x was between 1 and 12, our else statement got printed as well. Let s go back to the flow chart (shown on the right) to see why. As we can see here, our else statement will follow a flow based on the condition given in the if statement directly above it. Because we ve created a series of 12 if statements, the else statement doesn t necessarily know which if statement it is based off of. As a result, it will simply default to the if statement directly above it. Since the else statement was applicable only to the if x == 12: statement, it checked if "December" or "Number not Valid" should be printed. Let s look into how we can edit our code so that our else statement would be applicable to all of the if statements and would get printed if and only if the value of x is not between 1 and 12.

12 The Elif Statement Let s start off fixing the problem using what we already know. We could fix this by inserting our followup if statements inside of the predecessing if statement. I ll demonstrate this sort of solution with the first three months: Note: This is the WRONG way to fix our problem! By using the code above, our else statement that prints Not within the first 3 months is applicable to all the if statements. As you can see here, we were able to print out exactly what we wanted and expected by creating some complicated code with a complicated structure of if statements inside the else statement of our previous if (That s a mouthful, demonstrating that we may be overcomplicating things!). As you might imagine, our program can become really unreadable if we tried to add a lot of if statements in this manner. To get around this, we can write an else if statement all in one line. In Python, we can represent this else if by writing the following: elif <Boolean>: So let s take a look at how our code will now flow before we apply this to our program.

13 With this diagram, the first thing to note is that we don t need to constantly indent our code every time we want to add another elif. As we see, our code will run down our long if and check each condition until it finally finds a condition which turns out being True. If it doesn t find a condition that s true, then it ll just go ahead and run the else body just like it did before. What s important to keep in mind though, is that as soon as we find a condition that is True, we will not bother going through any of our other elif or else statements.

14 Below, is an example that will show how when one of our conditions are met, the rest of the conditions we did not go through yet will then be disregarded. Here, we printed out "Made it!" just like we d expect. We did not go through our second elif even though that condition would be True. Let s go ahead and apply this logic to our program now. Awesome! Our program now seems to work. We should probably do more thorough testing just to make sure. In the next session, we ll learn to use loops in order to do exactly that.

15 Loops When testing our code, we ll want to go through a wide variety of different possible cases. If possible, we want to test each condition that is in our code and then see if there are any edge cases which can cause our code to break. So in this case, we should probably test the numbers from 1 to 12 and then also a couple numbers greater than 12 and numbers less than 1. Let s start off by writing a couple of test values into list form. x = [-7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 55] Now, while we can reference each individual element in the list manually, it can get really tedious and things could also get very complicated and near impossible when we re dealing with very long lists of indeterminate size. In programming, we can use loops to go over a body of code as many times as we need to. Below is a diagram depicting how our code will flow. If this doesn t make sense, don t worry. We ll go through an example to apply this model. The first thing we should note is that this code here looks very similar to the if statement. We have our condition, which we then indent in order to convey what will happen should our condition be True. Here s where we start things start to become a little different though. With our loop, we will execute our Body of Repeating Code, but then go back to our condition. If it s True, we ll run through our body again and continue to repeat the cycle until our boolean value is False. Let s run through an example of when and why we might want to use code. Let s start off by creating a countdown that goes from 5 to 1. Originally, we would probably write our program like this:

16 As we can see, this took up quite a few lines. More importantly though, making changes to this would not be very easy. What if we wanted to change our countdown to countdown from 10 instead? We would probably have to add more print statements that will print the correct number. Let s adjust our code to do a countdown starting from 10 using a loop now. So with the loop, everything looks a lot more compressed. Let s go through this problem and apply the diagram from before so that we can fully comprehend how it works.

17 So I started off by defining a variable x, which will keep track of where I am in my countdown. Afterwards, I enter my loop with the condition that I will stay in my loop so long as x > 0. Inside the body, I lln print x and afterwards decrease x by one. Keep in mind that this will only occur when x is greater than 0 precisely because that s the condition I ve set. So I ll go ahead and repeat these steps as x decrements. I ll go through the body a total of 10 times, once when x = 10, when x = 9, x = 8, and onwards up until x = 0. Once I ve decremented and x has been decremented all the way to 0, my loop s condition now False. Now that x is not greater than 0, I ll go ahead and execute on the rest of my code, printing out "Blastoff!" As we can see here, it becomes very easy to make changes to the code now. If I want to do a countdown starting at 1000, I just need to change the starting value of x to be 1000 and the loop will do the rest of the work for me! Let s go ahead and use our for loop in order to test if our month interpreter works (Note: you ll have to go to the next page in order to see the solution!).

18

19 I ve run the code above and I ve got a bunch of numbers. On a closer look, it seems like the results seem to overall look correct. Let s take a closer look at how we went through every element of our list so that we could print the output. In this case, there are 3 critical lines of code that we want to take a close look at in order to get a better understanding. I will go ahead and write the code below: 1) index = 0 2) while index < len(my_list): #len will fetch the length of the list 3) index = index + 1 1) So here, I go ahead and use index to keep track of the element I want to look at within the list. I ll start with my first element, which is located at the 0th place, hence why I default index to the value 0. 2) The while loop is intended to be used to go through all the elements of my list. In order to achieve that goal, I just constantly check that index is less than the number of elements my_list. Remember, the moment that index == len(my_list), that means that index will no longer reference an element inside the list. For example, if there are 10 elements in the list, then the indices that reference those elements will number from ) In order to make sure that I continue to go through the list, I increment index by 1 to indicate that I m ready to move on to the next element and start over with my loop. So as you can see here, the loop is intended to help me test a couple values and see if my code is working properly. Luckily, the code seems to be working properly, but how can we have other programmers and users effectively use our code? Well, ideally, we want them to dictate to us what the values that are passed in to our month interpreter will be. Let s go ahead and take a look at the next section to see how we can use functions to effectively do that.

20 Functions In the section on if statements, we wrote a fully functional program that told us the month based on a number input. We then took advantage of loops in the following section to write tests for the program so that we can make sure that it s working as intended. Now we have one glaring problem that we want to deal with: how can we have other people take full advantage of this program? Ideally we want them to simply tell the program a number. Our program will then interpret the number and spit out the corresponding month to that number. Functions allow us to do exactly that. A function can be likened to a magical generator. The generator will need to be fed in some components, and based off those components, it ll go ahead and do work. Once all that work is done, the generator will give me back a resulting product: the output. So the solution that we want to act on is to create a product that will function as shown in the diagram to the right. I ll go ahead and build my month interpreter function so that I can have other programmers and users use this function by referencing it and passing in a number as input. Based on that number, I ll give back the name of the month. Let s take a look at the diagram below to see how functions work. So as we can see here, we go ahead and indicate that we re writing a function through the keyword, def. Afterwards, go ahead and give your function a unique name of your own choosing and specify how many inputs you will have by creating variables for each input. Here is a sample function definition below: Now that I ve created my function, I can go ahead and reference it by simply stating the name and passing in inputs. Once that happens, the function will go ahead and run the code inside. Using the keyword, return, it ll then pass my intended output over to what called the code so that it can

21 be used. Here is an example of how I would call the function above and afterwards print out the results: As we can see here, I went ahead and called the function by calling its name. I passed in three inputs like the function asks for (those values are stored into variables named a, b, and c). The function then did work and fed me an output, which I stored in my variable, x, which I then proceeded to print out so that we could see the results of our code. Let s go ahead and take our month interpreter solution and place it inside a function, which we ll call month_interpreter (Note: you ll have to go to the next page in order to see the solution!)

22 I went ahead and copied the original function we wrote without test cases into the body of a function. Finally, I made sure to replace all of my print statements with return statements so that it will properly provide an output that can be used by the caller of the function. Let s talk about the benefits to using a function here now. Whereas we had to previously write the block of code within where we want to use it, we can now simply reference the name of the function to use it. This offers a lot more flexibility for us as we write code others can use. Now, instead of having to understand the code inside, the user simply needs to understand the intentions of the function in order to effectively use it. In essence, we have effectively abstracted our code away, so that people only need to worry about the big picture.

23 Conclusion Praise the sun! We have effectively condensed what is often taught over the span of a couple weeks into about 23 pages of content, so great job, keep at it and I hope you took things away from here! You should now be familiar with the basics of programming. These concepts will have helped prepare you for the Introduction to Object Oriented Programming course, which will delve into the concept of abstraction, which we briefly touched upon in the Functions section. If you would like to learn more about various concepts in Computer Science though, please feel free to check out the Introduction to Computer Science course as well! Good luck and stay Udacious!

### Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Hello World! Computer Programming for Kids and Other Beginners by Warren Sande and Carter Sande Chapter 1 Copyright 2009 Manning Publications brief contents Preface xiii Acknowledgments xix About this

### Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 18 Tries Today we are going to be talking about another data

Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 01 Lecture - 04 Downloading and

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

Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

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

Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

### Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Getting Started Excerpted from Hello World! Computer Programming for Kids and Other Beginners EARLY ACCESS EDITION Warren D. Sande and Carter Sande MEAP Release: May 2008 Softbound print: November 2008

### Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller Table of Contents Introduction!... 1 Part 1: Entering Data!... 2 1.a: Typing!... 2 1.b: Editing

### MITOCW watch?v=0jljzrnhwoi

MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

### The Big Python Guide

The Big Python Guide Big Python Guide - Page 1 Contents Input, Output and Variables........ 3 Selection (if...then)......... 4 Iteration (for loops)......... 5 Iteration (while loops)........ 6 String

### Variables and Data Representation

You will recall that a computer program is a set of instructions that tell a computer how to transform a given set of input into a specific output. Any program, procedural, event driven or object oriented

### The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

### XP: Backup Your Important Files for Safety

XP: Backup Your Important Files for Safety X 380 / 1 Protect Your Personal Files Against Accidental Loss with XP s Backup Wizard Your computer contains a great many important files, but when it comes to

### Programming with Python

Programming with Python Dr Ben Dudson Department of Physics, University of York 21st January 2011 http://www-users.york.ac.uk/ bd512/teaching.shtml Dr Ben Dudson Introduction to Programming - Lecture 2

### Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Chapter 5 Using the Integers In spite of their being a rather restricted class of numbers, the integers have a lot of interesting properties and uses. Math which involves the properties of integers is

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

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 18 Switch Statement (Contd.) And Introduction to

### Text Input and Conditionals

Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

### Shadows in the graphics pipeline

Shadows in the graphics pipeline Steve Marschner Cornell University CS 569 Spring 2008, 19 February There are a number of visual cues that help let the viewer know about the 3D relationships between objects

### The second statement selects character number 1 from and assigns it to.

Chapter 8 Strings 8.1 A string is a sequence A string is a sequence of characters. You can access the characters one at a time with the bracket operator: The second statement selects character number 1

### CME 193: Introduction to Scientific Python Lecture 1: Introduction

CME 193: Introduction to Scientific Python Lecture 1: Introduction Nolan Skochdopole stanford.edu/class/cme193 1: Introduction 1-1 Contents Administration Introduction Basics Variables Control statements

C++ By A EXAMPLE Memory Addressing, Binary, and Hexadecimal Review You do not have to understand the concepts in this appendix to become well-versed in C++. You can master C++, however, only if you spend

### Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Fundamentals We build up instructions from three types of materials Constants Expressions Fundamentals Constants are just that, they are values that don t change as our macros are executing Fundamentals

### Table of Laplace Transforms

Table of Laplace Transforms 1 1 2 3 4, p > -1 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Heaviside Function 27 28. Dirac Delta Function 29 30. 31 32. 1 33 34. 35 36. 37 Laplace Transforms

### Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs

What are we going to cover today? Lecture 5 Writing and Testing Programs Writing larger programs Commenting Design Testing Writing larger programs As programs become larger and more complex, it becomes

### 2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Remembering numbers (and other stuff)... Let s talk about one of the most important things in any programming language. It s called a variable. Don t let the name scare you. What it does is really simple.

### Variables, expressions and statements

Variables, expressions and statements 2.1. Values and data types A value is one of the fundamental things like a letter or a number that a program manipulates. The values we have seen so far are 2 (the

### (Refer Slide Time: 00:23)

In this session, we will learn about one more fundamental data type in C. So, far we have seen ints and floats. Ints are supposed to represent integers and floats are supposed to represent real numbers.

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

C H A P T E R 7 In the first few chapters, we saw some of the basic building blocks of a program. We can now make a program with input, processing, and output. We can even make our input and output a little

### (Refer Slide Time 6:48)

Digital Circuits and Systems Prof. S. Srinivasan Department of Electrical Engineering Indian Institute of Technology Madras Lecture - 8 Karnaugh Map Minimization using Maxterms We have been taking about

### Functions and Decomposition

Unit 4 Functions and Decomposition Learning Outcomes Design and implement functions to carry out a particular task. Begin to evaluate when it is necessary to split some work into functions. Locate the

### Python for Non-programmers

Python for Non-programmers A Gentle Introduction 1 Yann Tambouret Scientific Computing and Visualization Information Services & Technology Boston University 111 Cummington St. yannpaul@bu.edu Winter 2013

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

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 15 Branching : IF ELSE Statement We are looking

### Divisibility Rules and Their Explanations

Divisibility Rules and Their Explanations Increase Your Number Sense These divisibility rules apply to determining the divisibility of a positive integer (1, 2, 3, ) by another positive integer or 0 (although

### Introduction to Programming

CHAPTER 1 Introduction to Programming Begin at the beginning, and go on till you come to the end: then stop. This method of telling a story is as good today as it was when the King of Hearts prescribed

### CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch Purpose: We will take a look at programming this week using a language called Scratch. Scratch is a programming language that was developed

### Program Planning, Data Comparisons, Strings

Program Planning, Data Comparisons, Strings Program Planning Data Comparisons Strings Reading for this class: Dawson, Chapter 3 (p. 80 to end) and 4 Program Planning When you write your first programs,

### MITOCW watch?v=kz7jjltq9r4

MITOCW watch?v=kz7jjltq9r4 PROFESSOR: We're going to look at the most fundamental of all mathematical data types, namely sets, and let's begin with the definitions. So informally, a set is a collection

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

These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

### Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 01 Lecture - 03 Euclid's Algorithm

### 2 Sets. 2.1 Notation. last edited January 26, 2016

2 Sets Sets show up in virtually every topic in mathematics, and so understanding their basics is a necessity for understanding advanced mathematics. As far as we re concerned, the word set means what

### (Refer Slide Time: 01.26)

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture # 22 Why Sorting? Today we are going to be looking at sorting.

### C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

C++ Data Types Contents 1 Simple C++ Data Types 2 2 Quick Note About Representations 3 3 Numeric Types 4 3.1 Integers (whole numbers)............................................ 4 3.2 Decimal Numbers.................................................

### Computer Science Lab Exercise 1

1 of 10 Computer Science 127 - Lab Exercise 1 Introduction to Excel User-Defined Functions (pdf) During this lab you will experiment with creating Excel user-defined functions (UDFs). Background We use

### Programming Fundamentals and Python

Chapter 2 Programming Fundamentals and Python This chapter provides a non-technical overview of Python and will cover the basic programming knowledge needed for the rest of the chapters in Part 1. It contains

### CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

CS125 : Introduction to Computer Science Lecture Notes #11 Procedural Composition and Abstraction c 2005, 2004 Jason Zych 1 Lecture 11 : Procedural Composition and Abstraction Solving a problem...with

### Arrays/Branching Statements Tutorial:

Arrays/Branching Statements Tutorial: In the last tutorial, you created a button that, when you clicked on it (the onclick event), changed another image on the page. What if you have a series of pictures

### 1.7 Limit of a Function

1.7 Limit of a Function We will discuss the following in this section: 1. Limit Notation 2. Finding a it numerically 3. Right and Left Hand Limits 4. Infinite Limits Consider the following graph Notation:

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

3 T H E I N T E R A C T I V E S H E L L The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform. Ada Lovelace, October 1842 Before

### Understanding Recursion

Understanding Recursion Brian L. Stuart February 23, 2015 It has been suggested that the single most original contribution that the field of Computer Science has made to the tapestry of human intellect

### Binary, Hexadecimal and Octal number system

Binary, Hexadecimal and Octal number system Binary, hexadecimal, and octal refer to different number systems. The one that we typically use is called decimal. These number systems refer to the number of

### Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Comp 11 Lectures Mike Shah Tufts University June 26, 2017 Mike Shah (Tufts University) Comp 11 Lectures June 26, 2017 1 / 57 Please do not distribute or host these slides without prior permission. Mike

### Sets. Sets. Examples. 5 2 {2, 3, 5} 2, 3 2 {2, 3, 5} 1 /2 {2, 3, 5}

Sets We won t spend much time on the material from this and the next two chapters, Functions and Inverse Functions. That s because these three chapters are mostly a review of some of the math that s a

### CS50 Supersection (for those less comfortable)

CS50 Supersection (for those less comfortable) Friday, September 8, 2017 3 4pm, Science Center C Maria Zlatkova, Doug Lloyd Today s Topics Setting up CS50 IDE Variables and Data Types Conditions Boolean

### MITOCW watch?v=se4p7ivcune

MITOCW watch?v=se4p7ivcune The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

5 Ways to Get More Use from VLOOKUP ALL RIGHTS RESERVED VLOOKUP Hack #1: Sort Order Issue VLOOKUP Hack #1 helps address the sort issue. Sort issue? Yes, and the sort issue has confuzzled many an Excel

### How Your First Program Works

How Your First Program Works Section 2: How Your First Program Works How Programs Are Structured...19 Method Main ( )...21 How Programs Are Structured In Section 1, you typed in and ran your first program

### MITOCW watch?v=flgjisf3l78

MITOCW watch?v=flgjisf3l78 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To

### (Refer Slide Time 3:31)

Digital Circuits and Systems Prof. S. Srinivasan Department of Electrical Engineering Indian Institute of Technology Madras Lecture - 5 Logic Simplification In the last lecture we talked about logic functions

### Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide

### Outlook is easier to use than you might think; it also does a lot more than. Fundamental Features: How Did You Ever Do without Outlook?

04 537598 Ch01.qxd 9/2/03 9:46 AM Page 11 Chapter 1 Fundamental Features: How Did You Ever Do without Outlook? In This Chapter Reading e-mail Answering e-mail Creating new e-mail Entering an appointment

### CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise If you re not already crazy about Scheme (and I m sure you are), then here s something to get

### Functions, Pointers, and the Basics of C++ Classes

Functions, Pointers, and the Basics of C++ Classes William E. Skeith III Functions in C++ Vocabulary You should be familiar with all of the following terms already, but if not, you will be after today.

### Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Faculty of Mathematics Waterloo, Ontario N2L 3G1 Centre for Education in Mathematics and Computing Relations Let s talk about relations! Grade 6 Math Circles November 6 & 7 2018 Relations, Functions, and

### Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;

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

Get JAVA To compile programs you need the JDK (Java Development Kit). To RUN programs you need the JRE (Java Runtime Environment). This download will get BOTH of them, so that you will be able to both

### Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 04 Lecture - 01 Merge Sort (Refer

### 4. Java Project Design, Input Methods

4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

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

Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

### Lecture Transcript While and Do While Statements in C++

Lecture Transcript While and Do While Statements in C++ Hello and welcome back. In this lecture we are going to look at the while and do...while iteration statements in C++. Here is a quick recap of some

### Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Coding Workshop Learning to Program with an Arduino Lecture Notes Table of Contents Programming ntroduction Values Assignment Arithmetic Control Tests f Blocks For Blocks Functions Arduino Main Functions

### Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14 Scan Converting Lines, Circles and Ellipses Hello everybody, welcome again

### Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Preface Here are my online notes for my Algebra course that I teach here at Lamar University, although I have to admit that it s been years since I last taught this course. At this point in my career I

### Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Writing a Simple Java Program Intro to Variables Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch

### Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.

### How to approach a computational problem

How to approach a computational problem A lot of people find computer programming difficult, especially when they first get started with it. Sometimes the problems are problems specifically related to

### Hey there, I m (name) and today I m gonna talk to you about rate of change and slope.

Rate and Change of Slope A1711 Activity Introduction Hey there, I m (name) and today I m gonna talk to you about rate of change and slope. Slope is the steepness of a line and is represented by the letter

### Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts Simple Data Types There are a number of data types that are considered primitive in that they contain only a single value. These data

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

MITOCW Lecture 10A [MUSIC PLAYING] PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between all these high-level languages like Lisp and the query

### CS 142 Style Guide Grading and Details

CS 142 Style Guide Grading and Details In the English language, there are many different ways to convey a message or idea: some ways are acceptable, whereas others are not. Similarly, there are acceptable

### Be careful when deciding whether to represent data as integers or floats, and be sure that you consider all possible behaviors in computation.

Table of Contents: 1. Integers and floats 2. for vs. while loops 3. Checking boolean conditions with if/else 4. Docstrings 5. Changing collections while iterating over them 6. Directly Accessing Instance

### Math Day 2 Programming: How to make computers do math for you

Math Day 2 Programming: How to make computers do math for you Matt Coles February 10, 2015 1 Intro to Python (15min) Python is an example of a programming language. There are many programming languages.

### Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Chapter01.fm Page 1 Monday, August 23, 2004 1:52 PM Part I The Mechanics of Change The Mechanics of Change Chapter01.fm Page 2 Monday, August 23, 2004 1:52 PM Chapter01.fm Page 3 Monday, August 23, 2004

### Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Overview Language Basics This chapter describes the basic elements of Rexx. It discusses the simple components that make up the language. These include script structure, elements of the language, operators,

### CS1 Lecture 3 Jan. 22, 2018

CS1 Lecture 3 Jan. 22, 2018 Office hours for me and for TAs have been posted, locations will change check class website regularly First homework available, due Mon., 9:00am. Discussion sections tomorrow

### 6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

### Macros in sbt: Problem solved!

Macros in sbt: Problem solved! Martin Duhem, Eugene Burmako Technical Report January 2015 Contents 1 Introduction 2 1.1 What problems do macros bring?................ 2 1.1.1 The problems we addressed

### TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

### COPYRIGHTED MATERIAL. Starting Strong with Visual C# 2005 Express Edition

1 Starting Strong with Visual C# 2005 Express Edition Okay, so the title of this chapter may be a little over the top. But to be honest, the Visual C# 2005 Express Edition, from now on referred to as C#

### Introduction to Programming with JES

Introduction to Programming with JES Titus Winters & Josef Spjut October 6, 2005 1 Introduction First off, welcome to UCR, and congratulations on becoming a Computer Engineering major. Excellent choice.

### SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/18/14

600.363 Introduction to Algorithms / 600.463 Algorithms I Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/18/14 23.1 Introduction We spent last week proving that for certain problems,

### Post Experiment Interview Questions

Post Experiment Interview Questions Questions about the Maximum Problem 1. What is this problem statement asking? 2. What is meant by positive integers? 3. What does it mean by the user entering valid

### (Python) Chapter 3: Repetition

(Python) Chapter 3: Repetition 3.1 while loop Motivation Using our current set of tools, repeating a simple statement many times is tedious. The only item we can currently repeat easily is printing the

### At full speed with Python

At full speed with Python João Ventura v0.1 Contents 1 Introduction 2 2 Installation 3 2.1 Installing on Windows............................ 3 2.2 Installing on macos............................. 5 2.3

### CS 4349 Lecture October 18th, 2017

CS 4349 Lecture October 18th, 2017 Main topics for #lecture include #minimum_spanning_trees. Prelude Homework 6 due today. Homework 7 due Wednesday, October 25th. Homework 7 has one normal homework problem.

### Table of Contents. 1. Cover Page 2. Quote 3. Calculated Fields 4. Show Values As 5. Multiple Data Values 6. Enroll Today!

Table of Contents 1. Cover Page 2. Quote 3. Calculated Fields 4. Show Values As 5. Multiple Data Values 6. Enroll Today! "It is Kind Of fun to do the IMPOSSIBLE" Walt Disney Calculated Fields The purpose

### Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras (Refer Slide Time: 00:17) Lecture No - 10 Hill Climbing So, we were looking

### How to Read AWStats. Why it s important to know your stats

How to Read AWStats Welcome to the world of owning a website. One of the things that both newbie and even old time website owners get overwhelmed by is their analytics and understanding the data. One of

### Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #06 Loops: Operators We have seen comparison operators, like less then, equal to, less than or equal. to and