PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.

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

ARDUINO CLUB Session 1: C & An Introduction to Linux.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
True or false A variable of type char can hold the value 301. ( F )
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
The If/Else Statement, Boolean Flags, and Menus Page 180
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Introduction to Python
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Variables and Expressions CMSC 201 Chang (rev )
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
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.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Data Handling in Algorithms. Activity 1 Starter Task: Quickly complete the sheet 5mins!
GCSE Computing: Programming GCSE Programming Remembering Python.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Whatcha doin'? Aims: To start using Python. To understand loops.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Variables, Expressions, and IO
Introduction to C++ October 2, 2017.
Type Conversion, Constants, and the String Object
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Computational Thinking
Computational Thinking
Chapter 2 Edited by JJ Shepherd
Primitive Types and Expressions
Unit 3: Variables in Java
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.
Data Types and Maths Programming Guides.
Class code for pythonroom.com cchsp2cs
Hardware is… Software is…
Presentation transcript:

PYTHON: PART 2 Catherine and Annie

VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s move on to using variables.  Do you remember using variables in Alice? You can do the same thing in Python. Variables are used to store information so that it can be used throughout a program  In order to create a variable in Python, you use the following syntax = value Example: numStudents = 10

MORE VARIABLES  It is important when naming variables to choose a name that is meaningful. You don’t want to name a variable that is supposed to represent the number of students in a class ‘x’. Name it something like ‘numStudents’  It is a convention in computer science to use something called camel case. This is when you use a combination of upper and lower case to denote words. For example, Students in numStudents is capitalized because it is a separate word from num.  Lines that contain a variable name, an equal sign, and a value are called initialization statements. A variable is created and immediately given a value so that it is not an empty part of the computer’s memory.  Let’s check out a program that makes use of variables

FIRSTVARIABLES.PY  Open the file called “firstVariables.py”  What will this program do? How can you tell?  Let’s run it and find out. Press F5 to run your program.

MORE OF FIRSTVARIABLES.PY  There are a few things we haven’t seen before in this program. Let’s look at them.  Each of the initialization statements looks something like = input(“Please enter a number: ”) The input method is built into Python, and it is a way to get input from the user The part in quotations is called a prompt, and it will display to the screen. Prompts are used to let the user know what kind of information they should be inputting  The last print statement looks kind of strange too. %d is a placeholder. The computer knows to expect an integer wherever the %d occurs. % sum lets the computer know that the we want the variable sum’s value to be printed.

A WORD ABOUT DATA TYPES  There are different ways to represent information. Numbers and words, for example. A computer uses data types to represent different kinds of, well, data!  Some common data types are:  Int (short for integer): whole numbers  Float: numbers with decimal points INCLUDING numbers like 5.0  Strings: a collection of characters (letters, punctuation, spaces)  Booleans: true or false

MORE ON DATA TYPES  Python is a little funny when it comes to division involving two integers. This glitch is known as integer division Here’s how it works: we all know that 5 (an integer) divided by 2 (an integer) is 2.5 (a float). HOWEVER: Python will only look at the integer part of the answer (2) and give it back to you if you ask it to perform that calculation (this is called truncating)  So how do we fix this problem? Type in 5.0 / 2.0 Python will see that you are dividing two floats and give you the correct (float) answer back.

THE MATH LIBRARY  Python only has a certain number of mathematical operations that are built into the language They are addition, subtraction, multiplication, division, exponents, and modulus  If we want to do anything else, like taking the square root of something, or doing any trigonometry, we need to import the math library  But what does it mean to import a library?  You write a line of code at the top of your program, before main, which instructs the computers to look for the file named and use it in your program

MORE OF THE MATH LIBRARY  To use the math library, write the following line of code at the top of a program: import math  Some of the function of the Python math library are: sqrt(x) – finds the square root of x pi – the mathematical constant e – the mathematical constant floor(x) – finds the closest integer less than x ceil(x) – finds the closest integer greater than x

YOUR TURN!  Open the file called “Python – Lesson 2 Exercises” and complete the exercises