Writing a Basic Program in Python

Size: px
Start display at page:

Download "Writing a Basic Program in Python"

Transcription

1 Writing a Basic Program in Python Introduction When someone wants to learn how to program a computer they could be seeking a number of different things. Someone way want to learn a particular programming language, such as Java or C++, because they have a project at work that requires some level of knowledge of these programming languages. They may want to understand how computer work at the programming-language level because they want to extend their knowledge of computer systems. They may want to understand the process of designing and creating a computer program irrespective of any particular programming language. These are all important topics but before learning a computer language you should have a clear goal about what you wish to learn. If you only wish to learn the syntax of a particular programming language then you can get a list of the language syntax rules. However, this won t teach you how to design a computer program. Learning how computer work in terms of programming languages does not really require knowledge of a particular language. This lecture will cover topics in all three areas; how computers relate to programming languages, the steps for designing and writing a computer program, and the syntax of the Python programming language. What is a Programming Language? Before we start looking at a particular language you should first understand what a programming language is used for and how this relates to an actual computer. You can copy down a computer program from a book or notes and likely get it to run, but if you don t understand what is actually happening inside the computer you might as well be following instructions to perform a magic spell. A Programming Language is a way for someone (the programming) to communicate with a computer. The Programming Language consists of a set of instructions that the computer understands. When the computer is given your instructions it will attempt to carry them out. Concept One: A Programming Language is a way to communicate with a Computer System Since computers are general-purpose machines with the ability to follow instructions, if we want a computer to do something we have to be able to give it instructions. This is done by using a Programming Language. But why use a programming language? Why not just speak or type my instructions to the computer? Why do I have to use a Programming Language? This is actually a very good question. Modern computers are at once very complicated and very simple. They are complicated in their abilities (everything now has a computer including coffee pots, rice cookers, automobiles, etc) but very simple in the way they can follow instructions. A Computer is a very literal-minded device and it will only do what you EXACTLY what you tell it to do. The reason computes are very simple-minded when it comes to carrying out instructions is that they are designed to be general purpose machines. To use an example, suppose you order coffee each morning before work. When you stop into the coffee shop you order your coffee with milk and one packet of sugar. If you do this same order enough the people in the shop that recognize you will likely fix your coffee as you like it. However, you are not the only customer. Other people may like their coffee a different way. If the coffee shop is designed to be used by everyone (designed to be a general-purpose machine) then there will be a procedure for asking customers what they want in their coffee. This may seem like a simple-minded way to figure out how people want their coffee, but to accommodate everyone a simple procedure must be used. In the same way a general-purpose computer must have a standardized way to get instructions. Most microwave ovens have simple computers that can be communicated with via pushing buttons to set time. These computers don t have a programming language because they can only perform a set of simple actions (cooking, defrosting, etc) which don t require a complex way of issuing commands. However, general purpose computers such as a PC or Mac can perform an

2 almost infinite set of tasks. Such systems require a very general-purpose way for people to interact with the system and so require a structured programming language. Concept Two: A Structured Programming Language is necessary to communicate with general-purpose computer systems. How does the computer understand your Instructions? To recap: Some Computers that are general-purpose systems General-purpose systems must be instructed in a simple but precise way Programming language are simple and precise We know that we must communicate with a general-purpose systems such as a computer using a programming language, but how does the computer understand my instructions? The way that a computer system uses the instructions in a programming language depends on the type of language being used to write these instructions. There are thousands of programming languages, but only two general types of languages; compiled and interpreted. Each type of programming language can be used to write instructions for a computer system and each can make the computer perform the same actions. However, the way that the instructions get to the computer system are different. The two general types of programming languages are Compiled and Interpreted. If we could peer into the guts of a computer as it ran a computer program, we would see a series of 1 s and 0 s (or voltage values). Computers are binary devices and they only understand instructions written as a series of 1 s and 0 s. Computer programs must, at some stage, be converted into these 1 s and 0 s. Both Interpreted and Compiled programs end up in this form before being carried out by the Computer. With an Interpreted programming language your instructions are converted to 1 s and 0 s dynamically by another program. Each line of an Interpreted program, as it is entered, is converted and sent to the computer for execution. With Compiled programming languages the entire set of commands, the entire program, is converted to the 1 s and 0 s format before the entire thing is sent to the computer. There are a variety of benefits and drawbacks to each method of converting instructions to 1 s and 0 s. The main benefit of the Compiled method is that the programs converted in this way tend to run faster than Interpreted programs. For an analogy think about going to the grocery store. Back in the old days, you would stand in front of a store keeper and read off the list of items you wanted to purchase. Each time you read an item from the list the storekeeper would go back, get the items, and ring up the purchase. You would then read the second item and this process would repeat. The way most people shop for grocery items is like how a compiled program works. All the items to be purchased are gathered together into a basket and then all items purchased at once at the checkout counter. It is more efficient than the single item process, but not as convenient because you have to collect the items yourself. An Interpreter is the program that takes each individual command of an Interpretative Language (such as Python), converts it to a set of 1 s and 0 s, and sends this command to the computer system. The Interpreter then waits for the next command. A Compiler is a program that reads from a text file containing all the instructions, converts the entire file to a set of 1 s and 0 s, and sends the results to the computer system. Compiler based languages include C and C++ while Interpreter based languages include JavaScript and Python. While compiler based programs tend to run faster, programs written with an Interpreter tend to be easier to create and are simpler in structure than compiler languages. Concept Three: Programming Languages are either Interpretive or Compiled but create the same type of binary code which is used by the computer system.

3 Concept Four: Interpreted languages are often easier to use but not as powerful as Compiler languages. Designing a Computer Program To recap; we communicate with a general purpose computer system by using a programming language. The programming language is either interpreted into directly executable commands or compiled into such commands. But what are we communicating to the computer system? What we are creating are a set of commands we wish the computer system to follow. These commands are collected into a Computer Program. There are two main aspects to learning a programming language; learning the syntax of the programming language and learning how to design a computer program. The latter skill is generic enough so you can write computer programs using different languages. If you create a generic enough design then the programming language used to implement this design is mostly irrelevant. Designing a computer program is like creating a plan. Suppose you want to give someone your Famous Peppermint Cookie recipe. This would entail writing down the ingredients and the steps for preparing the cookies. The steps would have to be precise enough so someone else could duplicate your cookies and would also have to be in the right order (mixing the dough before cooking and not after). Let s look at a Peppermint Cookie recipe (references to: ) Serves: 12 Ingredients 1/2 cup (1 stick) unsalted butter, softened 3/4 cup sugar 1 egg 1/4 tsp natural peppermint extract 1 1/4 cup unbleached all purpose flour (or whole wheat pastry) 1/3 cup cocoa powder 1/4 tsp salt 1/2 tsp baking soda 1/4 tsp baking powder 1 candy cane, finely crushed 1/3 cup dark, bittersweet or semisweet chocolate chips Instructions 1. Preheat oven to 350 degrees F. 2. In a large bowl, cream the butter and sugar together for about 1-2 minutes. Add egg, peppermint extract and beat until light and fluffy, scraping down the sides as you go to ensure even mixing. 3. Add flour, salt, cocoa powder, baking soda and powder to a sifter and sift gradually over the wet ingredients mixing as you go. Mix until well combined. 4. Add chocolate chips and stir in with a mixing spoon and then chill in the freezer for 10 minutes while the oven finishes heating. 5. Form the dough into small balls (heaping 1 Tbsp) and place 1.5 inches apart on an ungreased or parchment-lined baking sheet. Press the tops down and then top with a few more chocolate chips (to indicate what's inside). 6. Bake for minutes - the edges should start to dry but the tops will still be slightly soft looking. Let set on the cookie sheet for a few minutes more but sprinkle crushed candy cane on immediately. 7. Transfer to a cooling rack to rest until completely cooled. Store in an airtight container to keep fresh for up to a few days or freeze for several weeks.

4 And here is a picture of a finished cookie: This recipe (as with most recipes) includes both data and instructions. The data (ingredients) provide the context for the Instructions. Also, notice that both the information and the order of the Instructions are important. The other feature of this recipe are the assumptions about what the reader knows. This includes heating the oven, forming dough into small balls, etc. When designing a computer program you cannot assume the computer knows how to do anything not captured by the programming language. The general steps for creating a computer program, written in a particular language, are: 1. Write down a single paragraph describing what you want the program to do. This is important because it focuses the problem. 2. Write down the information you might be using in this program. 3. Write down the steps, in order, you want the computer to perform in non-programming language words. You don t have to use the programming language at this point. 4. Review your program description and the steps to ensure that you have captured what your program will do. 5. For each step, write down the programming language instruction to implement this step. If you are not yet sure how to write the step in the programming language then use a placeholder name. 6. Review your programming language steps. 7. Run your program. Designing a computer program involves moving from the abstract/general (the paragraph describing your program) to the specific (the programming language steps). You design a computer program by knowing where you are going (the program description) and how you will get there (the non-programming language steps) before attempting the journey (actually writing and running the code). Concept Five: Designing a computer program involves moving from the general to the specific in a stepwise fashion.

5 Computer Language Syntax and the Python Language The term Syntax means the rules of a language. This includes the rules for writing a sentence, when to capitalize, and what order to put words (among other things). The term is used pretty much the same way when applied to computer languages. Since computer languages are much simpler than natural languages such as English or Spanish, the syntax rules are much simpler. Learning the complete syntax of a computer language before you can use it to write programs is not necessary. All you have to learn are some simple starting rules. As you need to use more of the programming language to perform more complex tasks you will learn the syntax. While the term syntax is typically used to describe the language rules, it also describes the data types which can be used in a computer language. That is, the types of data which can be included in the program can be considered part of the language syntax. Remember the cookie recipe previously introduced. One part of the recipe was the list of ingredients. These were given in Cups and Tablespoons rather than in metric (grams and milliliters). One of the syntax rules of this recipe was that quantities would be given in cups, teaspoons, etc. Concept Six: The Syntax of a computer language describes not only the rules for writing down commands but the types of data that can be manipulated by the language. The Python language syntax is outlined on numerous Language Reference web pages (see here for one such page). However, you can simplify learning the language by dividing python commands into some general categories. These include: Variables and Data Types Expressions Decision Structures Looping Structures Methods These various python areas will be examined in this and later lectures. Sample Program One Getting Started Let s to a warm-up program so you can test out your environment. You should have already downloaded and installed the Python Interpreter and the PyCharm IDE (see the Instructors web page for these instructions). 1. Start up PyCharm and select Create a New Project. 2. Click the Create button to create a new Project. 3. Click in the Location text box and change the project name from untitled to gettingstarted. 4. Make sure the Interpreter references an installed version of Python. 5. Click the Create button. 6. Click the File menu and choose New. 7. Select Python File and give this file the name getstarted and click OK. You will use these same steps to start all your Python projects so remember how to get to this point. You should see an empty document with author = xxxx The Editor window is on the right where you will enter your program. Python is a column based programming language. This means you must make sure that commands line up and are in the correct column. 8. Click at the end of the line author = xxx and press Enter. 9. Type the following lines of code:

6 age = 22 print("i am",age,"years old") age = age + 10 print("in ten years I will be",age,"years old") 10. Click the Run menu and choose Run (the item with the green symbol). You will be prompted to select the project you wish to run. 11. Click the option for the project you are working on. The output of your program is displayed at the bottom of the screen. You have just written a Python program, sent the program to the computer, and are viewing the results. What are the commands in your program? The first command (after the author statement) is:

7 age = 22 This is a Python statement that assigns a value (22) to a Variable (age). What is a variable? You can think of variables as storage holes that can hold information. In this case age holds the number 22. The other thing to understand is that this is a Python Statement. This means that the combination of symbols and values is something Python can understand. In this case, the statement is an assignment statement since a value is being put into a variable. The equal symbol (=) when placed after a variable will assign whatever is on the right side of the equal. In fact, the right side of the expression is first evaluated before being assigned. For example, suppose you replace age = 22 with age = 20+2 The expression (20+2) on the right side of the equal symbol is first evaluated before the results are assigned to the variable. This means that the calculation 20+2 is first performed and the results assigned to the age variable. The second statement in your program is: print("i am",age,"years old") This python expression uses the print() method to display something on the screen (in this case, the window in PyCharm). What is a method? A method is one or more executable statements that typically perform a single task. When you use a method you are replacing the list of statements with a single name, in this case print. Many methods are written so they can accept information. The information passed into the method is placed between () symbols and written after the method name. The statement above will cause some information to be displayed as output. When you write a python statement that includes a method you are said to be calling the method. Another way to think about methods is that methods are like black-boxes. The executable statements inside the method are hidden, but we understand how to use the method. While we don t know what goes on inside a method we can list the two ways we can call a method and the two results that can come out of a method. The two ways to call a method are by sending it some data, which is done by writing something between the () symbols after the method name, or by calling the method with no data. Calling the print method with: print("i am",age,"years old") sends some data into the print-method black box. The print method will then display the data on the screen. Calling the same method without any data is done with: print() Notice that you still keep the () symbols but don t put anything between these symbols. This still calls the method but without sending it some data to print. What happens? In this case a blank line is printed.

8 There are also two possible outcomes from calling a method. These are: The method does something such as print a message, update a database, etc The method returns some data to the program Methods could to both of these. (We will learn more about methods in a later lecture.) If you look closer at the statement: print("i am",age,"years old") you notice that there are actually three items being sent to the method. Each item is separated with commas. The three items are: "I am" age "years old" Two of these are enclosed in quotation marks. This indicates that the item is a String value. The item named age is a variable we have previously assigned the value 22. How does the print() method use the data it is being passed? If the data item is enclosed in quotation marks then the print statement just displays it as it was passed (minus the quotation marks). If the item is not enclosed in quotation marks then the print() method assumes it is a variable and the method displays the contents of the variable. This is a very important idea. When you use variables in python statements the value of the variable and not the name of the variable are used. If we enclosed the variable age in quotation marks and called the print() method with: print("i am","age","years old") Then what would be printed would be: I am age years old Which would not be what we wanted. The other interesting feature of this print() method is that each item is separated with a comma. Each item in this call to the print() method is called a parameter. Some methods allow you to enter a defined number of parameters and other methods allow you enter an unlimited number of parameters. Look at the next few lines of code: age = age + 10 print("in ten years I will be",age,"years old") You understand the second print() statement because it is almost like the first print() statement, but the text has been changed. But what about the statement: age = age + 10

9 We have already seen how variables are assigned values from the statement: age = 22 In this case the variable named age is set with the value 22. We can understand the statement: age = age + 10 if we understand two of the Python syntax rules. These are: Values on the right side of an equal are assigned to the variable named on the left side of the equal and any existing data in the variable is replaced. Expressions on the right side of an equal are first evaluated before the results are assigned to the variable on the left side of the equal sign This means that before the contents of the age variable are changed the expression on the right side of the equal sign is evaluated and the results of the evaluation are assigned to the variable on the left side. Looking at this statement we can write down the steps python goes through: 1. Look at the expression on the right side of the equal. 2. If the right side of the equal is an expression then evaluate the expression. 3. Get the value of the variable age (which was set to 22 in a previous statement). 4. Add the number 10 to whatever value we get from age (22+10 gives us 32). 5. Save the results of evaluating the expression in a temporary holding spot. 6. Assign this value (in this case 32) to the variable age and replace whatever was already in this variable. This may sound like a lot of work, but this is what happens when we write: age = age + 10 After this statement the new value of age is 32, which will be printed by the last line of this program. The entire program is: age = 22 print("i am",age,"years old") age = age + 10 print("in ten years I will be",age,"years old") Variables and Data Types When learning a computer language you must learn the rules of the language. These rules are called the language syntax and they define what you can and cannot do when you write programs in the language. Two of the areas you must learn about are the rules for variables and the language data types. As you have seen from the sample program, a variable is a location in memory which can be used to hold data. How does Python know you are using a variable and how to label the place in memory which will be used to save information? This happens when you first use a variable. Notice in the sample program the first instance of the variable age was: age = 22

10 Your program did not have to inform the Python interpreter that you would be using a variable named age to store a number. The first time this variable was assigned a value, python automatically created a memory location with this name. In other computer languages you have to define variables before you can use them, but in Python all you all you have to do is create a python statement where the variable is assigned a value. (If you try and use the print() method to display the contents of a variable you have not yet assigned with a value, the Python Interpreter will issue an error flag. Variable Names and Rules As we have seen, a variable is a named area of memory which can hold data. We assign values to variables by putting the variable name on the left side of an equal sign and putting what we want to insert into the variable on the right side of the equal sign. While you can choose (almost) any name you wish for variables there are some python language rules you must follow. These are: Variable Rule One: Variable names cannot duplicate programming statements or methods. Variable Rule Two: Variable names are case sensitive. Variable Rule Three: Variable names must start with letters and cannot contain spaces or operators. Reserve Words There are a set of programming language commands which are used to write Python programs. These include words like for and if. These are called reserve words and cannot be used as variable names. The reason is that the Python interpreter cannot tell if the word if is being used as a variable name or as a programming command. The reserve words in python which cannot be used to name variables are: These would make poor variable names even if we could use them. When naming a variable you want to choose a name that identifies what the variable contains or what it will be used for. In the first sample program the variable named age contained a number which represented an age. Variable Name Case The second variable name rule says that variable names are case sensitive. This means that if you choose a variable name and then use it again it has to be the same name and have the same case. For example, suppose we rewrote our first program so it looks like: age = 22 print("i am",age,"years old") age = Age + 10 print("in ten years I will be",age,"years old") Notice the change. The third line adds 10 to the variable named Age with a capital A. If you tried to run this program Python would display an error indicating that the variable Age is not defined. This is because the two variables:

11 Age age are different variables. When writing python programs make sure you use the same variable name and case. The third variable naming rule says that variable names must start with Letters and that such names cannot include spaces or operators. For example, the following are invalid python variable names: $age 44Age My Age My-Age (because the minus sign is an operator) My$Age (because the dollar sign is an operator) The following are valid python variable names: My_Age MyAge age The safest way to follow this rule on naming variables is to only use letters and underscores in variable names. Assigning Variables From the first sample program you have seen how variables are assigned. This is done by putting either some value or expression on the right side of an equal sign and a variable name on the left. If the variable has been previously used then the contents are erased and replaced with whatever is on the right side of the equal sign. If the variable has not been previously assigned, then Python creates a memory location to hold some data and attaches the variable name to the memory location. The simplest way to assign some data to a variable is with a single equal sign. Whatever is on the left side of the equal is assigned to the right side. If the left side contains an expression then the expression is evaluated and the results are assigned. For example: membername = Frank memberage = 44 memberphone = memberpay = These four statements create and assign four variables, membername, memberage, memberphone, and memberpay. Notice that the phone number was written inside quotes so the exact number would be assigned. What would have happened if the quotes were not used? The assignment would look like: memberphone = which would be the results of subtracting 1212 from 515 (not a phone number). Variable assignments such as these can be made with one variable per line or can be combined: membername, memberage, memberphone, memberpay = Frank, 44, , A list of variable names, separated by commas, is placed on the left side of the equal sign and the values to be assigned to those variables placed on the right side of the equal sign in the order they are to be assigned. This last part is important. The data on the right side of the equal sign and the variable names on the left must match the order you wish assigned.

12 You can also assign multiple variables the same value by putting one value on the right side of an equal sign and multiple variables on the left. For example: memberapay, memberbpay, membercpay = Data Types The second thing you likely start with when learning a programming language are the allowable data types the language recognizes. A Data Type identifies the type of data that can be handle by the programming language. According to the official language syntax Python recognizes the following data types: Numbers String Boolean Bytes List Tuple Dictionary We will only be interested in Numbers, Strings, and Booleans and will cover the other data types in a later lecture. While numbers seem to be a single type of data, there are actually several types of numbers that python recognizes. These are: Integers Floating Point Long Complex We are only going to deal with Integers and Floating Point numbers at this point. The main difference between Integers and Floating Point numbers is that Floating Point numbers have a decimal point and Integers do not. The reason that Python (and most other computer languages) separate these two kinds of numbers has to do with how the numbers are stored. When a variable is defined and assigned either an Integer or Floating Point number the variable is tagged with the number type. Some operations won t work with Integers or Floating Point numbers so python must keep track of what type of number is stored in the variable. Creating an Integer variable or a Floating Point variable is as simple as just assigning a value to a variable: numberemployees = 12 averagepay = 8.55 If a number is to be Floating Point but does not have any value less than 1 then you would still include the decimal point: averagepay = 9.0 Another data type commonly used in Python are Strings. A String is a value enclosed in double quotes or single quotes. Strings are easy to define and there are a number of operations you can perform on Strings. As with numbers, you define a string variable by assigning it a value:

13 membername = Frank membername = Frank When you are assigning Strings to variable any spaces enclosed inside the or symbols will be preserved. There are a number of string operations you can perform but the most commonly used is the concatenation operation. This is like the addition operation on numbers, but it combines strings by sticking them together. For example: firstname = Joe lastname = Smith fullname = firstname + lastname print(fullname) This program creates three variables. The last variable, fullname, is created by sticking together the two names. What is printed is: JoeSmith Since a space was not included as part of the last name or first name, the combination does not automatically leave spaces. If you wanted to create a fullname variable that included a space you would write something like: fullname = firstname + + lastname which would contain Joe Smith Expressions and Calculations A python expression is a statement that can be executed by the python interpreter. For example: lastname = Smith age = 44 age = age + 10 are all python expressions. The following are not valid expressions that will not be rejected: age Smith python will not so much as complain about these expressions but will just ignore them. Some valid expressions that the interpreter will reject are: 44 = age Smith / 4 Calculations Calculations are done by combining numbers and variables with calculation symbols. The calculation symbols in python are: + - addition and subtraction / // division returning a float and division discarding remainder

14 * multiplication ** raise to the power % return the remainder of a division Along with these calculation symbols you can use () symbols to group calculations together. For example: payrate = hours = 40 totalpay = payrate * hours monthlypay = weeklypay = monthlypay / 4 weeklypay = (40*12.55) + ( (12.55*1.5) * 5) Look at the last statement. This calculates weekly pay by first multiplying 40 by (hourly pay rate) and then adding time and a half (12.55*1.5) times 5 hours overtime. Notice the placement of the () symbols. Several other calculation symbols you may not be as familiar with are: x = 5 ** 2 x = 12 % 7 x = 12 // 7 The first is raise to the power. The results of 5 ** 2 is 5 squared which is 25. The second is remainder from a division. The % operator returns the remainder of a division. For example, dividing 12 by 7 has a remainder of 5 (7 goes into 12 once with a remainder of 5). The // symbol returns a division result but strips off all decimal values. For example 12 // 7 returns 1 12 / 7 returns Using Methods We have already discusses how methods work. A method is a set of executable code that has a name. Methods can be called and passed information. The information passed into a method is placed inside () symbols and placed after the method name. For example, the method for printing data is: pay = * 40 print( Your pay is, pay) Parameters to Methods Methods can be called and passed data or just called without passing data. In the later case, even if there is nothing to pass to the method, you would still use the () after the method name: print()

15 Depending on the method, you must supply the appropriate parameters in the right order. For example, suppose you have a method name calcovertime() that will calculate the overtime pay of an employee. This method requires the base pay and the total number of hours the employee worked. You must pass these two numbers to the method in the right order: basepay = totalhours = 55 overtime = calcovertime( basepay, totalhours) Results from Methods Look at the previous set of python expressions. Notice that the method calcovertime() is placed on the right side of the assignment symbol = This means that the method calcovertime() returns a value which can then be assigned to a variable. This language syntax (returning a value from a method) is one of the rules of the python language. Methods that return a value can be used on the right side of an assignment symbol (the = ) and whatever the method returns will be put into a variable or treated as a value. For example, suppose we had a method that calculates overtime and this method was named calcovertime(). We could put the call to this method in any position we would normally expect a variable or value: print( The overtime pay is, calcovertime( basepay, totalhours) ) Understanding how Python handles methods is a fundamental idea when learning python since there are a number of built-in or added libraries to Python. Sample Program Two Calculating your Birthday Let s put what we have learned together and write a Python program to calculate how many days old you are when you enter your birthdate. This program will use the following built-in Python methods: date( year, month, day) date.today() this returns a Date object for the indicated date this returns a Date object for today Python uses a number of built-in library files which can be used to write programs. These libraries must be imported into your program before they can be used. This import process basically tells the Interpreter which libraries you want to use. Once you have imported the libraries you can then reference the methods and Objects in this library. Up to this point you have learned how to call a method and that methods can be passed parameters and that they will either do something or return a value. Another item in Python libraries are Objects. An Object is a complex entity that contains information and has embedded methods. You can reference already existing objects in the library or you can use one object to create your own objects. This will be covered in a later lecture, but you can still use objects and object methods if you understand the rules. In this case, the date() method creates and returns a Date object. This is an Object that represents a particular date. It has an internal structure consisting of the month, day, and year you used to create the object. The syntax of this object is: mydate = date(1971, 4, 22) which will create a date object for April 22, To create a date object for today you can use the method: todaysdate = date.today()

16 Once you create Date objects you can perform some simple calculations. For example, subtracting a date object for some date from a date object for day gives you a new object that contains the difference between these two dates. For example: mybirthday = date(1971,4,22) todaydate = date.today() diff = todaydate mybirthday You can now call some methods inside the diff object to get the number of days and seconds between the two dates. 1. Start Pycharm 2. If you already have a project open click the File menu and choose Close Project. 3. Click Create New Project option to create a new project. 4. Click in the Location text box and type the name Birthday for the project name. 5. Click the Create button to create the project. 6. Click the File menu, choose New, and choose Python File. 7. For the file name type birthday and click OK. You must first import the libraries. 8. Below the author line type: from datetime import date Your program will first create a date object for some date, create a date object for today, and then subtract one from the other. 9. Type: mybirthday = date(1971,4,22) todaysdate = date.today() diff = todaysdate - mybirthday where the date is Year, Month, Day in numeric format. You will next get the number of days and seconds in the diff object 10. Type: numdays = diff.days numsecs = diff.total_seconds() numyears = numdays//365 print("you are",numyears,"old and",numdays,"days and",numsecs,"seconds") The diff variable contains a set of values which results from subtracting today s date from the birthday date. Since diff is a complex object it contains several internal pieces. These can be retrieved with diff.days and diff.total_seconds(). To get the number of years we divide numdays by 365 and use // to throw away any decimal value.

17 Finally, the print statement prints out the variables you created. The complete program looks like: from datetime import date mybirthday = date(1971,4,22) todaysdate = date.today() diff = todaysdate - mybirthday numdays = diff.days numsecs = diff.total_seconds() numyears = numdays//365 print("you are",numyears,"old and",numdays,"days and",numsecs,"seconds") 11. Click the Run menu and choose Run. Select your project to run the program. The output shows at the bottom of the page. If you have an error or the program does not work correctly The output for 1971, 4, 22 should be: You are 43 old and days and seconds This program will display the current years, days, and seconds from any earlier date to today. Input The sample program takes a date entered into the program and calculates the number of years, days, and seconds from today. However, each time you want to enter a new date you must type the date into the program and re-run the program. A better way is to include commands that, when the program runs, prompt the user to enter a date. One way to do this is by using the input() method. This is a method that will pause the program and ask the user to enter some data. This data is placed into a variable. The input command looks like: var = input( Enter some data ) which will display the prompt Enter some data and wait for the user to enter some data and press the Enter key. The problem with the input() method is that it only returns string values. This means that if a user enters a number value it is saved in the variable as a string. Before using the numbers from the input() commands you need to convert a string to a number. This is done with the int() method. Sample Program Three A Better Birthday Program 1. Make sure your previous project is open. 2. Click on the line after the from datetime import date line and press Enter to insert a blank line.

18 3. Enter the following lines of code: stryear = input( Enter your birthday year in four digits ) strmonth = input( Enter your birthday month in two digits ) strday = input( Enter your birthday day in two digits ) intyear = int(stryear) intmonth = int(strmonth) intday = int(strday) 4. Edit the line that creates the mybirthday variable so it reads: mybirthday = date(intyear, intmonth, intday) 5. The complete program should now look like: from datetime import date stryear = input("enter your birthday year in four digits ") strmonth = input("enter your birthday month in two digits ") strday = input("enter your birthday day in two digits ") intyear = int(stryear) intmonth = int(strmonth) intday = int(strday) mybirthday = date(intyear,intmonth,intday) todaysdate = date.today() diff = todaysdate - mybirthday numdays = diff.days numsecs = diff.total_seconds() numyears = numdays//365 print("you are",numyears,"old and",numdays," days and",numsecs,"seconds") Run this program and try out some dates. You will be prompted at the bottom of the screen to enter values.

Notes on Chapter 1 Variables and String

Notes on Chapter 1 Variables and String Notes on Chapter 1 Variables and String Note 0: There are two things in Python; variables which can hold data and the data itself. The data itself consists of different kinds of data. These include numbers,

More information

Graded Project. HTML Coding

Graded Project. HTML Coding Graded Project HTML Coding OVERVIEW 1 INSTRUCTIONS 1 SUBMITTING YOUR PROJECT 17 Contents iii HTML Coding OVERVIEW After completing the exercises for your HTML Coding study guide, you re now ready to complete

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

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

More information

COMPUTATIONAL THINKING

COMPUTATIONAL THINKING Computational Concepts sequence loops parallelism events conditionals operators variables lists COMPUTATIONAL THINKING PROGRAMMING AS PROBLEM SOLVING Computational Practices incremental/iterative testing/debugging

More information

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

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

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

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

More information

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

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

More information

COMP 102: Computers and Computing

COMP 102: Computers and Computing COMP 102: Computers and Computing Lecture 5: What is Programming? Instructor: Kaleem Siddiqi (siddiqi@cim.mcgill.ca) Class web page: www.cim.mcgill.ca/~siddiqi/102.html Motivation The advantage of a computer

More information

The diagram below is only partially complete. Use the terms in the word list to complete the empty boxes. Make dinner

The diagram below is only partially complete. Use the terms in the word list to complete the empty boxes. Make dinner Decomposing Problems As we have seen, decomposition means breaking down a difficult problem into more manageable pieces. A common way to tackle this process is to use a tree diagram showing increasing

More information

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour What is a program? Java Programming CS 160, Fall Semester 2012 TOPICS Computer Programs Using Eclipse Program Program Components Definition: a sequence of instructions telling a computer what to do Analogy:

More information

Week - 01 Lecture - 04 Downloading and installing Python

Week - 01 Lecture - 04 Downloading and installing Python 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

More information

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

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

More information

Memory Addressing, Binary, and Hexadecimal Review

Memory Addressing, Binary, and Hexadecimal Review 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

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 02: Algorithms Readings: Not Covered in Textbook Problem Solving Process System Design:

More information

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

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

More information

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CPS122 Lecture: From Python to Java last revised January 4, Objectives: Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

Variables and. What Really Happens When You Run hello_world.py

Variables and. What Really Happens When You Run hello_world.py 2 Variables and Simple Data Types In this chapter you ll learn about the different kinds of data you can work with in your Python programs. You ll also learn how to use variables to represent data in your

More information

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

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute Module # 02 Lecture - 03 Characters and Strings So, let us turn our attention to a data type we have

More information

Lesson 166 (Pages 27-29)

Lesson 166 (Pages 27-29) 0 Lesson Lesson (Pages -) Skills / Concepts Understand horizontal and vertical lines Begin memorizing += and += Class Preparation Flash Cards: Move += and += from UNUSED FACTS to NEW FACTS. Move CC FLASH

More information

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

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

More information

A Unicorn Named Sparkle Word Search. A New Friend for Sparkle Coloring Sheet. A Unicorn Named Sparkle s First Christmas Coloring Sheet

A Unicorn Named Sparkle Word Search. A New Friend for Sparkle Coloring Sheet. A Unicorn Named Sparkle s First Christmas Coloring Sheet Table of Contents Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Pages 8 9 Page 10 Page 11 Page 12 Page 13 Page 14 Page 15 Name Tags A Unicorn Named Sparkle Word Search Draw and Name Your Unicorn Connect

More information

Lecture 3. Input, Output and Data Types

Lecture 3. Input, Output and Data Types Lecture 3 Input, Output and Data Types Goals for today Variable Types Integers, Floating-Point, Strings, Booleans Conversion between types Operations on types Input/Output Some ways of getting input, and

More information

MITOCW watch?v=se4p7ivcune

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

More information

Variables, expressions and statements

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

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

Basic Programming Language Syntax

Basic Programming Language Syntax Java Created in 1990 by Sun Microsystems. Free compiler from Sun, commercial from many vendors. We use free (Sun) Java on UNIX. Compiling and Interpreting...are processes of translating a high-level programming

More information

the rules The Goal Get all three of your monkeys around the board and into the Banana Grove before anyone else can!

the rules The Goal Get all three of your monkeys around the board and into the Banana Grove before anyone else can! the rules Equipment Code Monkey Island Gameboard, 12 monkey figurines (three of each color), 54 Guide cards, 16 Fruit cards, 10 Boost in a Bottle cards. The Goal Get all three of your monkeys around the

More information

Binary, Hexadecimal and Octal number system

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

More information

The Big Python Guide

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

More information

Functions and Decomposition

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

More information

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

More information

Chapter 1 Operations With Numbers

Chapter 1 Operations With Numbers Chapter 1 Operations With Numbers Part I Negative Numbers You may already know what negative numbers are, but even if you don t, then you have probably seen them several times over the past few days. If

More information

LOOPS. Repetition using the while statement

LOOPS. Repetition using the while statement 1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while

More information

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007 CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007 Course Web Site http://www.nps.navy.mil/cs/facultypages/squire/cs2900 All course related materials will be posted

More information

6.001 Notes: Section 1.1

6.001 Notes: Section 1.1 6.001 Notes: Section 1.1 Slide 1.1.1 This first thing we need to do is discuss the focus of 6.001. What is this course all about? This seems quite obvious -- this is a course about computer science. But

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 17. Fundamental Concepts Expressed in JavaScript Chapter 17 Fundamental Concepts Expressed in JavaScript Learning Objectives Tell the difference between name, value, and variable List three basic data types and the rules for specifying them in a program

More information

4. Java Project Design, Input Methods

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

More information

GENERAL MATH FOR PASSING

GENERAL MATH FOR PASSING GENERAL MATH FOR PASSING Your math and problem solving skills will be a key element in achieving a passing score on your exam. It will be necessary to brush up on your math and problem solving skills.

More information

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore CS 115 Data Types and Arithmetic; Testing Taken from notes by Dr. Neil Moore Statements A statement is the smallest unit of code that can be executed on its own. So far we ve seen simple statements: Assignment:

More information

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Module 10A Lecture - 20 What is a function?

More information

The Dynamic Typing Interlude

The Dynamic Typing Interlude CHAPTER 6 The Dynamic Typing Interlude In the prior chapter, we began exploring Python s core object types in depth with a look at Python numbers. We ll resume our object type tour in the next chapter,

More information

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

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

More information

CPS122 Lecture: From Python to Java

CPS122 Lecture: From Python to Java Objectives: CPS122 Lecture: From Python to Java last revised January 7, 2013 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

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 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.

More information

CS 115 Lecture 4. More Python; testing software. Neil Moore

CS 115 Lecture 4. More Python; testing software. Neil Moore CS 115 Lecture 4 More Python; testing software Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 8 September 2015 Syntax: Statements A statement

More information

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Fall 2016 Lab 09 Advanced Debugging CMSC 201 Fall 2016 Lab 09 Advanced Debugging Assignment: Lab 09 Advanced Debugging Due Date: During discussion Value: 10 points Part 1: Introduction to Errors Throughout this semester, we have been working

More information

Programming Fundamentals and Python

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

More information

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s Intro to Python Python Getting increasingly more common Designed to have intuitive and lightweight syntax In this class, we will be using Python 3.x Python 2.x is still very popular, and the differences

More information

Introduction to Python Code Quality

Introduction to Python Code Quality Introduction to Python Code Quality Clarity and readability are important (easter egg: type import this at the Python prompt), as well as extensibility, meaning code that can be easily enhanced and extended.

More information

Chapter 2 Working with Data Types and Operators

Chapter 2 Working with Data Types and Operators JavaScript, Fourth Edition 2-1 Chapter 2 Working with Data Types and Operators At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics

More information

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel Introduction: PHP (Hypertext Preprocessor) was invented by Rasmus Lerdorf in 1994. First it was known as Personal Home Page. Later

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

More information

COPYRIGHTED MATERIAL. An Introduction to Computers That Will Actually Help You in Life. Chapter 1. Memory: Not Exactly 0s and 1s. Memory Organization

COPYRIGHTED MATERIAL. An Introduction to Computers That Will Actually Help You in Life. Chapter 1. Memory: Not Exactly 0s and 1s. Memory Organization Chapter 1 An Introduction to Computers That Will Actually Help You in Life Memory: Not Exactly 0s and 1s Memory Organization A Very Simple Computer COPYRIGHTED MATERIAL 2 Chapter 1 An Introduction to Computers

More information

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

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

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

More information

POWERONE TEMPLATES A DOCUMENT DESCRIBING HOW TO CREATE TEMPLATES.

POWERONE TEMPLATES A DOCUMENT DESCRIBING HOW TO CREATE TEMPLATES. I N F I N I T Y S O F T W O R K S POWERONE TEMPLATES A DOCUMENT DESCRIBING HOW TO CREATE TEMPLATES www.infinitysw.com/help/create Templates What is a template? powerone uses templates as its primary medium

More information

Expressions, Statements, Variables, Assignments, Types

Expressions, Statements, Variables, Assignments, Types Expressions, Statements, Variables, Assignments, Types CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of this material

More information

6.001 Notes: Section 8.1

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

More information

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley PHP and MySQL for Dynamic Web Sites Intro Ed Crowley Class Preparation If you haven t already, download the sample scripts from: http://www.larryullman.com/books/phpand-mysql-for-dynamic-web-sitesvisual-quickpro-guide-4thedition/#downloads

More information

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 07 Tutorial 2 Part 1 Facebook API Hi everyone, welcome to the

More information

CSE 142, Summer 2015

CSE 142, Summer 2015 CSE 142, Summer 2015 Lecture 2: Static Methods Expressions reading: 1.4 2.1 The Mechanical Turk 2 Escape Characters System.out.println( ab\ \\\\\\/\td ); Output: ab \\\/ d 3 Algorithms algorithm: A list

More information

Microsoft Access XP (2002) Queries

Microsoft Access XP (2002) Queries Microsoft Access XP (2002) Queries Column Display & Sorting Simple Queries And & Or Conditions Ranges Wild Cards Blanks Calculations Multi-table Queries Table of Contents INTRODUCTION TO ACCESS QUERIES...

More information

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 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

More information

USER S GUIDE. Model 8300

USER S GUIDE. Model 8300 USER S GUIDE Model 8300 4840 Hytech Drive Carson City, NV 89706 U.S.A. 1-800-854-8075 or 1-775-885-4900 Fax: 1-775-885-4949 E-mail: info@calculated.com www.calculated.com TABLE OF CONTENTS KEY DEFINITIONS...1

More information

COPYRIGHTED MATERIAL. Dipping Your Toe into Python. Part I. Chapter 1: Programming Basics and Strings. Chapter 2: Numbers and Operators

COPYRIGHTED MATERIAL. Dipping Your Toe into Python. Part I. Chapter 1: Programming Basics and Strings. Chapter 2: Numbers and Operators Part I Dipping Your Toe into Python Chapter 1: Programming Basics and Strings Chapter 2: Numbers and Operators Chapter 3: Variables Names for Values COPYRIGHTED MATERIAL 1 Programming Basics and Strings

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore (Refer Slide Time: 00:20) Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 4 Lexical Analysis-Part-3 Welcome

More information

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM 1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM 1.1 Introduction Given that digital logic and memory devices are based on two electrical states (on and off), it is natural to use a number

More information

CSE 142, Summer 2014

CSE 142, Summer 2014 CSE 142, Summer 2014 Lecture 2: Static Methods Expressions reading: 1.4 2.1 Algorithms algorithm: A list of steps for solving a problem. Example algorithm: "Bake sugar cookies" Mix the dry ingredients.

More information

[301] Bits and Memory. Tyler Caraza-Harter

[301] Bits and Memory. Tyler Caraza-Harter [301] Bits and Memory Tyler Caraza-Harter Ones and Zeros 01111111110101011000110010011011000010010001100110101101 01000101110110000000110011101011101111000110101010010011 00011000100110001010111010110001010011101000100110100000

More information

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2 Python for Analytics Python Fundamentals RSI Chapters 1 and 2 Learning Objectives Theory: You should be able to explain... General programming terms like source code, interpreter, compiler, object code,

More information

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Java Programming. What is a program? Programs as Recipes. Programs Recipes 8/20/15. Pancakes In one bowl

Java Programming. What is a program? Programs as Recipes. Programs Recipes 8/20/15. Pancakes In one bowl What is a program? Java Programming CS 160, Fall Semester 2012 TOPICS Computer Programs Using Eclipse Program Program Components Definition: a sequence of instructions telling a computer what to do Analogy:

More information

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output Last revised January 12, 2006 Objectives: 1. To introduce arithmetic operators and expressions 2. To introduce variables

More information

Programming with Python

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

More information

DOWNLOAD PDF MICROSOFT EXCEL ALL FORMULAS LIST WITH EXAMPLES

DOWNLOAD PDF MICROSOFT EXCEL ALL FORMULAS LIST WITH EXAMPLES Chapter 1 : Examples of commonly used formulas - Office Support A collection of useful Excel formulas for sums and counts, dates and times, text manipularion, conditional formatting, percentages, Excel

More information

Introduction to programming with Python

Introduction to programming with Python Introduction to programming with Python Ing. Lelio Campanile 1/61 Main Goal - Introduce you to programming - introduce you to the most essential feature of python programming 2/61 Before to start The name

More information

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

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,

More information

(I m not printing out these notes! Take your own.)

(I m not printing out these notes! Take your own.) PT1420 Week 2: Software Program Design I (I m not printing out these notes! Take your own.) Today we'll be discussing designing programs: Algorithms and errors Flowcharts and pseudocode Sequence structures

More information

Part II Composition of Functions

Part II Composition of Functions Part II Composition of Functions The big idea in this part of the book is deceptively simple. It s that we can take the value returned by one function and use it as an argument to another function. By

More information

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

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.

More information

(Refer Slide Time: 01.26)

(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.

More information

Variables and Constants

Variables and Constants 87 Chapter 5 Variables and Constants 5.1 Storing Information in the Computer 5.2 Declaring Variables 5.3 Inputting Character Strings 5.4 Mistakes in Programs 5.5 Inputting Numbers 5.6 Inputting Real Numbers

More information

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS Computers process information and usually they need to process masses of information. In previous chapters we have studied programs that contain a few variables

More information

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

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style CS125 : Introduction to Computer Science Lecture Notes #4 Type Checking, Input/Output, and Programming Style c 2005, 2004, 2002, 2001, 2000 Jason Zych 1 Lecture 4 : Type Checking, Input/Output, and Programming

More information

CS112 Lecture: Primitive Types, Operators, Strings

CS112 Lecture: Primitive Types, Operators, Strings CS112 Lecture: Primitive Types, Operators, Strings Last revised 1/24/06 Objectives: 1. To explain the fundamental distinction between primitive types and reference types, and to introduce the Java primitive

More information

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Programming Fundamentals - Day Instructor: Jason Yoon Website: Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused

More information

Yup, left blank on purpose. You can use it to draw whatever you want :-)

Yup, left blank on purpose. You can use it to draw whatever you want :-) Yup, left blank on purpose. You can use it to draw whatever you want :-) Chapter 1 The task I have assigned myself is not an easy one; teach C.O.F.F.E.E. Not the beverage of course, but the scripting language

More information

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Introduction to Java Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Introduce Java, a general-purpose programming language,

More information

(Refer Slide Time: 1:40)

(Refer Slide Time: 1:40) Computer Architecture Prof. Anshul Kumar Department of Computer Science and Engineering, Indian Institute of Technology, Delhi Lecture - 3 Instruction Set Architecture - 1 Today I will start discussion

More information

Section 1. The essence of COBOL programming. Mike Murach & Associates

Section 1. The essence of COBOL programming. Mike Murach & Associates Chapter 1 Introduction to COBOL programming 1 Section 1 The essence of COBOL programming The best way to learn COBOL programming is to start doing it, and that s the approach the chapters in this section

More information

Python for Non-programmers

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

More information

Grade 7 Math LESSON 14: MORE PROBLEMS INVOLVING REAL NUMBERS TEACHING GUIDE

Grade 7 Math LESSON 14: MORE PROBLEMS INVOLVING REAL NUMBERS TEACHING GUIDE Lesson 14: More Problems Involving Real Numbers Time: 1.5 hours Prerequisite Concepts: Whole numbers, Integers, Rational Numbers, Real Numbers, Sets Objectives: In this lesson, you are expected to: 1.

More information

How Your First Program Works

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

More information

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides Slide Set 1 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section

More information