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.

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

Lecture 2 Introduction to C Programming
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
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
Introduction to C Programming
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
An Introduction to Textual Programming
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Programming.
Introduction to Python
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Variables and Expressions CMSC 201 Chang (rev )
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Python Programming Using Variables and input. Objectives We’re learning to use basic knowledge of variables combined with user input. Outcomes Continue.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Decision Making CMSC 201 Chang (rev ).
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
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.
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.
Input, Output and Variables GCSE Computer Science – Python.
CMSC201 Computer Science I for Majors Lecture 03 – Variables
CST 1101 Problem Solving Using Computers
Agenda Introduction Computer Programs Python Variables Assignment
CMSC201 Computer Science I for Majors Lecture 02 – Intro to Python
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Introduction to Python
Data Types and Conversions, Input from the Keyboard
Line Continuation, Output Formatting, and Decision Structures
Chapter 2 - Introduction to C Programming
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Introduction to C++ October 2, 2017.
Variables, Printing and if-statements
Chapter 2 - Introduction to C Programming
Imperative Programming
Line Continuation, Output Formatting, and Decision Structures
Chapter 2 - Introduction to C Programming
CMSC 201 Computer Science I for Majors Lecture 02 – Intro to Python
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Data Types and Maths Programming Guides.
Introduction to C Programming
What you need to do… Drag the pieces of code into the correct order like you can see in the EXAMPLE below. print ( “ int input ) = + Hello world chr ord.
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Presentation transcript:

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 then execute it. You can also test simple python commands in the python interpreter.

Variables Last class we talked about variables being places with names that store information. In python, you can make a variable like this: someVariable = 10 This creates a variable, called someVariable, that has the value of 10. This value can change later on. The equals sign is called the assignement operator. It allows you to change the value of a variable.

Variables The right hand side of the assignment can be any mathematical expression. For example! someVariable = 10 anotherVariable= aThirdVariable= 10 * anotherVariable someVariable = someVariable + anotherVariable Whenever a variable is on the right hand side of an expression, imagine it being replaced by whatever value is currently stored in that variable.

Variables There are many different kinds of variables! Numbers (Integers or decimals) True / False values (called booleans) Strings (collections of characters) aString = ‘Hello everyone’ decimal = 1.12 bool = True

Rules for Naming Variables We have certain rules about what a variable can be named: Variable names are case sensitive. A variable named Hello and a variable named hello are regarded as different. Only may contain alphabetic letters, underscores or numbers. Should not start with a number. Cannot be any other python keyword (if, while, def, etc). You should always attempt to give your variables meaningful names!

When do I make variables? Variables are designed for storing information. Going back to our averaging example from earlier, when we were summing up the list we needed a place to put that sum as it was being generated. Any piece of information you wish your program to use or record must be stored in a variable. Think of it as giving that piece of information a name.

Output We’d like to see what’s stored in our variables! Python can print things for us: print(“Hello world”) You can also output the contents of variables: someVariable = 10 print(someVariable) You can even do combinations! print(“Your variable is “, someVariable)

Exercise What will the following code snippet print? a = 10 b = a * 5 c = “Your result is: “ print(c, b)

Exercise What will the following code snippet print? a = 10 b = a a = 3 print(b) There are two possible options for what this could do! Any guesses?

Exercise a = 10 b = a a = 3 print(b) It will print out 10. When you set one variable equal to another, they don’t become linked; b is set to 10 and no longer has anything else to do with a.

Input Sometimes, we’d like the user to participate! We can also get input from the user. userInput = input(“Please enter a number “) print(userInput) The output will look like this: Please enter a number 10 10

Input This line: userInput = input(“Please enter a number “) Takes whatever the user entered and stores it in the variable named “userInput” You can do this as many times as you like! userInput1 = input(“Please enter your first number.”) userInput2 = input(“Please enter your second number.”)

Input Note: All input will come as a string, a series of characters. There is a difference between “10” and the number 10. “10” is composed of two characters stuck together, while python understands 10 as a number. To turn an input into a number, you can do the following: someNumber = input(“Please enter a number: “) someNumber = int(someNumber) int stands for integer

Exercise Write, on paper or on your computer, a program that asks the user for two numbers a prints out the average.

Exercise Pretend you’re writing a program to compute someone’s weight grade. You have so far: hwWeight =.4 examWeight =.5 discussionWeight =.1 Write a program starting with these three lines that asks the user for their homework grade, exam grades, and discussion grades and prints out their total grade in the class.