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.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to Python
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;
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Java Data Types Data types, variable declaration, and initialization.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
CW-V1 SDD 0601 Principals of Software Design and Development Variables Constants Data Types Error Handling Starter: BlockbustersBlockbusters.
CPS120: Introduction to Computer Science
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Primitive Data Types. Identifiers What word does it sound like?
02 Variables1November Variables CE : Fundamental Programming Techniques.
1 homework Due today: hw #1 (mailing list printout) readings to date: chapter 1 and chapter read appendix B (3 pages on DOS) and and.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it?
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Mathematical Calculations in Java Mrs. C. Furman.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Data And Variables Chapter Names vs. Values Michael Jordan name (the letter sequence used to refer to something) value (the thing itself)
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Python Let’s get started!.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
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.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
A Sample Program #include using namespace std; int main(void) { cout
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
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.
Introduction to Python Lesson 2a Print and Types.
Input, Output and Variables GCSE Computer Science – Python.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
String Methods Programming Guides.
Python Let’s get started!.
Documentation Need to have documentation in all programs
Introduction to Python Data Types and Variables
Variables and Primative Types
Variables, Expressions, and IO
IDENTIFIERS CSC 111.
Variables ICS2O.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
The Data Element.
Primitive Types and Expressions
The Data Element.
Understanding 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.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Class code for pythonroom.com cchsp2cs
Variables and Constants
Presentation transcript:

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 value a name e.g. firstName  This is called a variable  A variable is a named area of memory that is used to hold some data whilst the program is running  We use the = assignment operator to put a value into this variable. E.g. firstName = “Susan”sport = “swimming” count = 10rate = of 18

Evaluating an Expression  These are all assignment expressions: value = amount * rate total = total + number mark = total / 100 Work out (evaluate) the part on the right-hand side of the = sign Assign the result to the variable on the left 3 of 18 value 150 If amount = 10 and rate = 15…

Data Types  A data type is a classification of the sort of value being represented.  A string is something in double or single quotes  An integer is a whole number  A floating point number has a decimal part  A boolean is either True or False 4 of 18

Displaying the Contents of a Variable  We can display the value being held in a variable:  Or do an operation first…  Try some examples…  Don’t forget to put something in the variables before you try to print them out… 5 of 18

Variables  Creating a new variable is called declaring a variable  You don’t need to tell Python what type of data you are going to use a variable for  However, it is poor practice to change it once you’ve started to use it  This will result in an error. 6 of 18

Identifiers  Names for variables, functions etc. are called identifiers  There are some rules for naming them: You cannot use special characters (e.g. space, &, !, $, * etc.) You cannot start an identifier with a number (e.g. 1stname) You cannot use reserved keywords (more of these later)  Variable names should be descriptive but not too long  These are all good examples: first_name, discountedPrice, favourite_sport  These are not: a, f1, the_users_favourite_sport 7 of 18

Exercises 1. Declare and initialise these variables: name, team Display a message like this: My friend supports 2. Declare and initialise these variables: favFilm, reason Display a message like this: I enjoyed because 3. Declare and initialise two variables to hold your friend’s name and age. Display a message showing your friend’s name and age. 8 of 18

Changing the Contents of a Variable firstName = “Susan” count = 10 total = 5 We can change a variable by:  Simply replacing the old value, e.g. firstName = “Fred” count = 12 total =  Incrementing it if it’s a number, e.g. count = count of 18

What can we do with strings?  A string isn’t just text, it has functionality…  A string can Tell you what it would look like in a different case… Try these… Here, we are calling the upper() function on the myText string variable 10 of 18

Be Careful…  What does variable name contain now?  The upper(), lower() etc. string functions do not change the value of the variable. How can we do this? 11 of 18

Exercise Declare and initialise two variables: firstName and surname Convert the surname to upper case Convert the firstName to title case Print them like this:, 12 of 18

What else can we do with strings?  Join strings together using a +. This is called concatenation.  You need to explicitly add a space between parts if necessary 13 of 18

Exercises 1. Cars Create two variables: colour and carModel and give them values. Join these strings together into a new variable called carDesc e.g. if colour is “red” and carModel is “VW Golf” then carDesc should contain“red VW Golf” 2. Shopping List Create three variables: item1, item2 and item3 and give them values Create a new variable: list which contains “Don’t forget to buy:” followed by all the items e.g. Don’t forget to buy: cheese, bread and tomatoes 14 of 18

Concatenation and Addition  Have a look at this program. What will be printed?  How about this one?  Why are they different?  What if one is a string and one is a number??! Try it… 15 of 18

More about Strings  We can also find out the number of characters in a string  Why are these used differently?  This is because the function len() can be used with things other than strings. The function upper() is specific to strings and is this functionality is part of what it is to be a string of 18

Exercise for Next Week 1. Store your first name in a variable 2. Store your surname in a different variable 3. Change the surname to uppercase 4. Display your full name in this format: SURNAME Firstname 5. Create a new variable called fullName 6. Concatenate your firstname and surname and store the result in fullName 7. Display the value of fullName 8. Display the length of the fullName 17 of 18

Summary  We have looked at: What variables are and why we use them How to evaluate an expression Datatypes Different things we can do with strings  For next week: Complete the exercise on the previous slide 18 of 18