Thinking about programming

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Python November 14, Unit 7. Python Hello world, in class.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to Python
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Introduction to Python
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Java Programming, Second Edition Chapter Two Using Data Within a Program.
ITEC 109 Lecture 7 Operations. Review Variables / Methods Functions Assignments –Purpose? –What provides the data? –What stores the data? –What type of.
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.
Early Program Design Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Input, Output and Variables GCSE Computer Science – Python.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
CS 100 Introduction to Computing Seminar September 12/14, 2016.
Thinking about programming
Chapter 1.2 Introduction to C++ Programming
CST 1101 Problem Solving Using Computers
Chapter 1.2 Introduction to C++ Programming
Computer Science 210 Computer Organization
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to Python
Formatting Output.
Introduction to Computer Science / Procedural – 67130
Introduction to Python and programming
Variables, Expressions, and IO
Thinking about programming
Functions CIS 40 – Introduction to Programming in Python
Type Conversion, Constants, and the String Object
Chapter 2: Introduction to C++
Thinking about programming
Chapter 2.
Introduction to Python and programming
Introduction to Python and programming
Number and String Operations
Thinking about programming
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Introduction to Primitive Data types
Variables Title slide variables.
Coding Concepts (Data- Types)
Introduction to Python and programming
Intro to Nested Looping
Lecture 5: Basic Java Syntax AP Computer Science Principles
Chapter 2: Introduction to C++.
Thinking about programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Python and programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Primitive Data types
Types, Truth, and Expressions (Part 2)
Getting Started in Python
Presentation transcript:

Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg

Last Time We wrote code to solve a problem How much Olympic athletes win Before we write code, we should think about the problem A design document lists the steps our program needs to take in English Each part of the design document can and should be used as comments in your program Should include inputs, calculations, and outputs

prizeMoney.py Design Document Identify the prize money for each medal type (constant over each athlete). Obtain the number of gold, silver, and bronze medals for the athlete (different for each athlete). For each medal type, multiply the medal count with its prize amount. Sum these three values to get total money won Display total amount of money won

Comments Comments help readers understand tricky pieces of code Good convention to place comments before each group of code that does one thing Denoted by # I will look for these in the next homework assignment

Constants We can now introduce constants Written in ALL_CAPS_STYLING Holds values that don’t change throughout your program Why would we want to use constants?

Variables and Types Python does not require you to pre-define the type of a variable What type a variable holds can change Unlike other programming languages… However, once a variable has a value it’s type matters a lot! Is x+3 legal? Thus proper naming is important!

Examples For example, you can’t use the + operator with a string and an int >>> “Sarah”+3 Python tries to convert one operand to the other operand’s type What about the + operator with a float and an int? >>> 4.1 + 3 >>> 3 + 4.1

Python “Types” integers: 5 floats: 1.2 Booleans: True (note the capital) Boolean named after Mathematician George Boole strings: “anything” or ‘something’ lists: [,]: [‘a’,1,1.3] others we will see

Use of quotation marks with String type Python allows the use of either Single quotes ‘This is a single quote sentence’ Double quotes “This is a double quote sentence” Triple quotes “”” These are most often used when writing comments that span over several lines.”””

What is a Type? A type in Python essentially defines two things: Internal structure of the data (what it contains) Kinds of operations you can perform Different methods are associated with different types of data >>> abc.capitalize() Method you can call on strings, but not integers Have you seen something like this before?

Converting Types A character ‘1’ is not an integer 1. We’ll see more on this later, but take my word for it. You need to convert the value returned by the input() command (characters) into an integer >>> inputString = “123” >>> inputInteger = int(inputString)

Type Conversion int(someVar) converts to an integer float(someVar) converts to a float str(someVar) converts to a string should check out what works: int(2.1)  2, int(‘2’)  2, but int(‘2.1’) fails float(2)  2.0, float(‘2.0’)  2.0, float(‘2’)  2.0, float(2.0)  2.0 str(2)  ‘2’, str(2.0)  ‘2.0’, str(‘a’)  ‘a’

Knowing the type type(variableName) Since the type of data stored in a variable can change, Python let’s us ask what the current type is: type(variableName)