Sep 12, 2007Sprenkle - CS1111 Objectives Review  Linux  Why programming languages? Compiled vs. Interpreted Languages Programming in Python  Data types.

Slides:



Advertisements
Similar presentations
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
String Escape Sequences
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Computer Science 101 Introduction to Programming.
CPS120: Introduction to Computer Science Lecture 8.
Introduction to Python
Introduction to Programming Professor Sprenkle September 10, 2007.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computer Science 101 Introduction to Programming.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Introduction to Computer Systems and the Java Programming Language.
Computer Science 101 Introduction to Programming.
CPS120: Introduction to Computer Science
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Sept 18, 2007Sprenkle - CS111 Lab 0 Feedback Great! Great! Details got some of you Details got some of you  Print out copy  Copy directory to turnin.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Variables, Expressions and Statements
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Python Let’s get started!.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Fundamentals of Programming I Overview of Programming
C++ First Steps.
Topics Designing a Program Input, Processing, and Output
Introduction to Python
Topic: Python’s building blocks -> Variables, Values, and Types
Revision Lecture
Variables, Expressions, and IO
IDENTIFIERS CSC 111.
Building Java Programs Chapter 2
CS 100: Roadmap to Computing
Topics Designing a Program Input, Processing, and Output
Introduction to Programming with Python
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to C Programming
Presentation transcript:

Sep 12, 2007Sprenkle - CS1111 Objectives Review  Linux  Why programming languages? Compiled vs. Interpreted Languages Programming in Python  Data types  Expressions  Variables  Comments  Arithmetic

Sep 12, 2007Sprenkle - CS111 Review: Linux How do you …  Learn more about a Linux command?  List the files in a directory?  Change your current directory?  Make a directory?  Find out the current directory? What is the shortcut for …  The current directory?  The parent directory?

Sep 12, 2007Sprenkle - CS111 Review: Linux File Structure usr etchome / coursesstudents www tmp faculty Your directories cs111 labs lab0 handoutsturnin Your web pages Paths through tree Relative paths

Sep 12, 2007Sprenkle - CS111 Why Do We Need Programming Languages? Computers can’t understand English  Too ambiguous (PB&J) Humans can’t easily write machine code Problem Statement (English) Algorithm/Pseudocode Bytecode High-level Programming Language (Python) Machine code/Central Processing Unit (CPU) Programmer (YOU!) translates from problem to algorithm (solution) to program Python interpreter translates into bytecode

Sep 12, 2007Sprenkle - CS111 Why Do We Need Programming Languages? Computers can’t understand English  Too ambiguous (PB&J) Humans can’t easily write machine code Problem Statement (English) Algorithm/Pseudocode Bytecode High-level Programming Language (Python) Machine code/Central Processing Unit (CPU) Python interpreter executes the bytecode in a “virtual machine”

Sep 12, 2007Sprenkle - CS111 Python Interpreter 1. Validates the Python programming language expression Enforces Python syntax Reports syntax errors 2. Simulates a computer (executes the expression) Runtime errors (e.g., divide by 0) Semantic errors (not what you meant) Interpreter (python) Python expression Output Executable bytecode Have a lot of these early on! Good way to test expressions What you were doing in the “shell”

Sep 12, 2007Sprenkle - CS1117 Our Programming Process 1. Programmer types a program/script into a text editor (jEdit or IDLE). 2. An interpreter turns each expression into bytecode and then executes each expression Interpreter (python) Program text file program.py Output Text Editor (jEdit or IDLE) Executable bytecode 1 “line” at a time Get feedback about which line caused the problem Interpreter stops validating/executing lines

Sep 12, 2007Sprenkle - CS111 Compiled Languages Examples: Java, C++, C, etc. Compile whole program into bytecode/executable format Then, execute the bytecode/machine code

Sep 12, 2007Sprenkle - CS1119 Compiled Language Programming Process 1. Compiler compiles program Validate program, report syntax errors Creates executable (bytecode or machine code) 2. Execute executable Compiler Program text file Output Text Editor Executable Bytecode or Machine code Validates whole program

Sep 12, 2007Sprenkle - CS111 Compiled vs. Interpreted Languages Compiler Program text file Output Text Editor Executable Bytecode or Machine code Interpreter Program text file Output Text Editor Executable bytecode 1 “line” at a time Compiled: Interpreted: Validates whole program + immediate feedback + execution speed

Sep 12, 2007Sprenkle - CS11111 Parts of an Algorithm  Primitive operations  What data you have, what you can do to the data Naming  Identify things we’re using Sequence of operations Conditionals  Handle special cases Repetition/Loops Subroutines  Call, reuse similar techniques An overview for the semester!

Sep 12, 2007Sprenkle - CS11112 Primitive Types Variable types are the kind of thing a variable can hold Broadly, the categories of primitive types are  Numeric  Boolean  Strings

Sep 12, 2007Sprenkle - CS11113 Numeric Primitive Types Data TypeDescriptionExamples int Plain integers (32- bit precision) -214, -2, 0, 2, 100 Range: to float Real numbers.001, , , 0.00, 2.45 long Bigger integers (neg or pos, precision limited by computer memory) L complex Imaginary numbers (have real and imaginary part) 1j * 1J --> (-1+0j) Copy on board

Sep 12, 2007Sprenkle - CS11114 How big (or small or precise) can we get? We cannot represent all values Problem: Computer has a finite capacity  The computer only has so much memory that it can devote to one value.  Eventually, reach a cutoff Limits size of value Limits precision of value In reality, computers represent data in binary, using only 0s and 1s PI has more decimals, but we’re out of space!

Sep 12, 2007Sprenkle - CS11115 Strings: str Indicated by double quotes “” or single quotes ‘’ Treat what is in the “” or ‘’ literally  Known as string literals Examples:  “Hello, world!”  “c”  “That is Sara’s cat”

Sep 12, 2007Sprenkle - CS111 Booleans: bool 2 values  True  False More on these later…

Sep 12, 2007Sprenkle - CS11117 What is the value’s type? ValueType j “int” L True ‘false’

Sep 12, 2007Sprenkle - CS11118 Introduction to Variables Variables have names, called identifiers A variable name (identifier) can be any one word that:  Consists of letters, numbers, or _  Cannot start with a number  Cannot be a Python reserved word like for, while, def, etc. Python is case-sensitive:  change isn’t the same as Change

Sep 12, 2007Sprenkle - CS11119 Variable Name Conventions Variables start with lowercase letter Constants (values that won’t change) are in all capitals Example: Variable for the current year  currentYear  current_year  current year  CURRENT_YEAR

Sep 12, 2007Sprenkle - CS111 Variable Semantics Computer Memory x = 5 y = x + 1 x 5 y 6

Sep 12, 2007Sprenkle - CS11121 Assignments Variables can be given any value using the “=“ sign  called an assignment statement Syntax: = After a variable is given a value, the variable is said to be initialized. These aren’t equations! Read “=” as “gets” current_year = 2007; my_num = 3.4; option = ‘q’;

Sep 12, 2007Sprenkle - CS11122 Variables: The Rules 1. Only the variable(s) to the left of the = change 2. You should initialize a variable before using it on the righthand side (rhs) of a rule. 3. You can only have one variable with any given name in a particular block.

Sep 12, 2007Sprenkle - CS11123 Literals Pieces of data that are not variables are called literals. Ex: ‘q’ “books”

Sep 12, 2007Sprenkle - CS111 Arithmetic Operations SymbolMeaning +Addition -Subtraction *Multiplication /Division %Remainder (“mod”) **Exponentiation (power)

Sep 12, 2007Sprenkle - CS11125 Arithmetic You can use the assignment operator (=) and arithmetic operators to do calculations. Remember your order of operations! (PEMDAS) The thing on the left gets changed. x = 4+3*10; y = 3.0/2.0; z = x+y; The right-hand sides are expressions, just like in math.

Sep 12, 2007Sprenkle - CS11126 Printing Output print is a function  Function: A command to do something  Displays the result of expression(s) to the terminal print “Hello, class” print “Your answer is”, answer string literal print automatically adds a ‘\n’ (carriage return) after it’s printed Use commas to print multiple “things” in one line

Sep 12, 2007Sprenkle - CS111 Getting Input From User input and raw_input are functions  Prompts user for input, gets the user’s input  input: for numbers  raw_input for strings Typically used in assignments  width = input(“Enter the width: “) In execution, terminal displays  “Enter the width: ”  Assigns width the value the user enters

Sep 12, 2007Sprenkle - CS111 Getting Input from User color = raw_input(“What is your favorite color? ”) > python input_demo.py What is your favorite color? blue Cool! My favorite color is _light_ blue ! Terminal: Grabs every character up to the user presses “enter” Assigns variable color that input

Sep 12, 2007Sprenkle - CS111 Documenting Your Code Use English to describe what your program is doing in comments  Everything after a # is a comment Color-coded in IDLE, jEdit  Python does not execute comments How to Use Comments  Document the author, high-level description of the program at the top of the program  Provide an outline of an algorithm Identifies the steps of the algorithm  Describe difficult-to-understand code

Sep 12, 2007Sprenkle - CS111 Identify the Pieces of a Program # Demonstrate numeric and string input # by Sara Sprenkle for CS111 on 9/12/07 # color = raw_input("What is your favorite color? ") print "Cool! My favorite color is _light_", color, "!" scale = input("On a scale of 1 to 10, how much do you like Matt Damon? ") print "Cool! I like him ", scale*1.8, " much!" Identify the comments, variables, functions, expressions, assignments

Sep 12, 2007Sprenkle - CS111 Identify the Pieces of a Program # Demonstrate numeric and string input # by Sara Sprenkle for CS111 on 9/12/07 # color = raw_input( "What is your favorite color? ” ) print "Cool! My favorite color is _light_”, color, "!" scale = input( "On a scale of 1 to 10, how much do you like Matt Damon? ” ) print "Cool! I like him ”, scale*1.8, " much!" Identify the comments, variables, functions, expressions, assignments, literals expression

Sep 12, 2007Sprenkle - CS11132 Practice Average three numbers