Variables In today’s lesson we will look at: what a variable is

Slides:



Advertisements
Similar presentations
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Advertisements

Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
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.
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
A Level Computing#BristolMet Session ObjectivesU2#S10 MUST describe the difference between constants, local and global variables SHOULD explain why constants.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
I Power Int 2 Computing Software Development High Level Language Constructs.
Variables and Expressions CMSC 201 Chang (rev )
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
Programming, an introduction to Pascal
Introduction to Programming
I Power Higher Computing Software Development High Level Language Constructs.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter Four Common facilities of procedural languages.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
7 - Programming 7J, K, L, M, N, O – Handling Data.
CompSci 230 S Programming Techniques
Numbers and arithmetic
Unit 2 Technology Systems
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Agenda Introduction Computer Programs Python Variables Assignment
Chapter 2 Variables.
Numbers and Arithmetic
CMSC201 Computer Science I for Majors Lecture 02 – Intro to Python
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Multiple variables can be created in one declaration
CS 1428 Exam I Review.
Computer Science 3 Hobart College
G7 programing language Teacher / Shamsa Hassan Alhassouni.
Lecture 8.
CMSC 201 Computer Science I for Majors Lecture 02 – Intro to Python
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Manipulating Text In today’s lesson we will look at:
Chapter 2 Variables.
Variables Title slide variables.
Margaret Derrington KCL Easter 2014
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
GCSE Computing Lesson 6.
Variables Kevin Harville.
Variables, Types, Operations on Numbers
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Chapter 2 Programming Basics.
Primitive Types and Expressions
C# Revision Cards Data types
Java: Variables, Input and Arrays
IAT 800 Foundations of Computational Art and Design
Chapter 2 Variables.
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.
CS 1428 Exam I Review.
Class code for pythonroom.com cchsp2cs
Variables and Constants
Python fundamental.
COMPUTING.
Introduction to Python
Presentation transcript:

Variables In today’s lesson we will look at: what a variable is different types of variables why we have types using variables in your program

Variables Variables are probably the most important thing in programming. They store the data that your program is using as it runs. Without them, it would be very difficult (if not impossible) to write a useful program. There are similarities between the way variables are used and algebra.

Types With most programming languages (but not all of them) variables have a type. Python has four main types – integer (whole number), float (number), Boolean (true or false) and string (text) The type of a variable in Python is set automatically based on the value you put in it, but you can force, e.g., a number to be stored as a string if you want to.

Variable Names Variable names are words or letters – just like algebra You can use any combination of letters and numbers, but variable names: must begin with a letter contain only letters and numbers must not be commands, e.g. PRINT is not a valid variable name

Examples What would be a good name for a variable to store the following data: Someone’s name Someone’s age The cost of postage A postcode The total price for an item A telephone number

Assignment You must give a variable a value before you use it, e.g. setting a score to zero. Giving a variable a value is know as assigning a value. You can use a single value or the result of a calculation, e.g. age = 10 age = 8 + 2 name = "Joe" fullname = "Joe " + "Bloggs" fullname = forename + " " + surname

Assignment age = age + 1 (age += 1 does the same) You can also use the same variable as part of the assignment - this is useful when adding to, or taking away from, a value: age = age + 1 (age += 1 does the same) fullname = "Mr " + fullname The following assignments is NOT valid and will result in a type mismatch error because you're trying to mix strings and numbers: name = “Andrew" name = name + 10

Examples name = “Andrew” print(name) name = “Hello ” + name What would be the output of this program? name = “Andrew” print(name) What about this one? name = “Hello ” + name

Examples What would be the output of this program? print a + b What are the values of a and b at the end? a = 4 b = 2 a = a + b b = b + 1

Casting Casting means changing the type of a variable Python will sometimes cast automatically (also known as coercion), e.g. to store the result of a calculation - sometimes you will get a type mismatch error or an unexpected result. e.g. if a = 1 and b = 2.5, a is an int to start with. Assigning a = a * b will make a equal to 2.5, and a becomes a float. But, if c = 1 and d = c/2, the value of d is 0 because d is also an int. However, e = d/2.0 would make e a float with the value 0.5!

Scope Variables declared in the main body of your program can be used anywhere in the program – these are called global variables Variables declared in a function can only be accessed in that function – these are called local variables. If you want to use a global variable in a function, use the global keyword

Constants Some programming languages have things called constants Constants are named values, like variables, but their value doesn’t change They can be used to simplify the use of mathematical constants, e.g. pi = 3.14159 Python doesn’t have constants, but you can use variables in the same way