Hello, World and Variables

Similar documents
LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Basics of Java Programming

>>> * *(25**0.16) *10*(25**0.16)

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

ENGR 101 Engineering Design Workshop

BASIC ELEMENTS OF A COMPUTER PROGRAM

Topic 2: Introduction to Programming

Data types Expressions Variables Assignment. COMP1400 Week 2

Algorithms and Programming I. Lecture#12 Spring 2015

Expressions and Variables

Full file at

Variable and Data Type I

R programming I expressions, variables, and data types. Luigi Cerulo University of Sannio

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

4. The is a diagram that graphically depicts the steps that take place in a program. a. Program b. Flowchart c. Algorithm d. Code e.

LECTURE 3 C++ Basics Part 2

Introduction to TURING

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Variable and Data Type I

EECS 280 C++ Coding Standards

Expressions in JavaScript. Jerry Cain CS 106AJ October 2, 2017

COMP 110 Introduction to Programming. What did we discuss?

3. Java - Language Constructs I

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Decisions, Decisions. Testing, testing C H A P T E R 7

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Programming Lecture 3

DATA AND ABSTRACTION. Today you will learn : How to work with variables How to break a program down Good program design

ISA 563 : Fundamentals of Systems Programming

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

PYTHON MOCK TEST PYTHON MOCK TEST III

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

The C++ Language. Arizona State University 1

MIT AITI Python Software Development

CSCE 120: Learning To Code

Text Input and Conditionals

CSE 142/143 Unofficial Style Guide

Full file at

Chapter 2. C++ Basics

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Using Basic Formulas 4

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

1/11/2010 Topic 2: Introduction to Programming 1 1

Variables, expressions and statements

Programming with Python

Object-Oriented Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CSCE 110 Programming I

Course Outline. Introduction to java

Building Java Programs

Computer Components. Software{ User Programs. Operating System. Hardware

1.8 Intro to Variables, Algebraic Expressions, and Equations

Python Unit

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Programming Fundamentals and Python

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Program Planning, Data Comparisons, Strings

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Chapter 2: Introduction to C++

Common Core State Standards Mathematics (Subset K-5 Counting and Cardinality, Operations and Algebraic Thinking, Number and Operations in Base 10)

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Operators. Lecture 3 COP 3014 Spring January 16, 2018

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

C++ Programming: From Problem Analysis to Program Design, Third Edition

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

ARITHMETIC EXPRESSION

Power Standard 5 Practice Test

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Arithmetic and IO. 25 August 2017

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Java Module Lesson 2A Practice Exercise

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CSCI 2101 Java Style Guide

Python notes from codecademy. Intro: Python can be used to create web apps, games, even a search engine.

Introduction to Python

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Python Programming Exercises 1

Lecture 1: Hello, MATLAB!

BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER. Chapter 2

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Lesson 1: Arithmetic Review

Working with JavaScript

Program Fundamentals

Lecture 3 Operators MIT AITI

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Computer Components. Software{ User Programs. Operating System. Hardware

Transcription:

Hello, World and Variables Hello, World! The most basic program in any language (Python included) is often considered to be the Hello, world! statement. As it s name would suggest, the program simply returns the phrase Hello, World!. In Python, this is done with the following line of code: Which will return the following: print() will return the text encased in quotation marks in the parentheses. The item in quotation marks is referred to as a string, which is just a sequence of written characters. For example, Hello is a string of H, e, l, l, and o. But, print() can output numbers as well: We can also print out two strings added together: It may seem like it would be easier to put spameggs as the string to print, and in this example it would be, but once we start working with variables, it will become very useful. This is one of the primary examples of program output. It is incredibly valuable for the vast majority of programs. Try it yourself : print out your name. Variables Another valuable tool for programming is the variable. A variable is a representation of a number, string, or other piece of data. The representation is a letter or word that is assigned the given data with the = operator: 1

Variables can be used in place of the data for the same effect: Some variables do not contain a string or a number, but instead contain either True or False. These are known as boolean values : Booleans are particularly useful as Flags which can be used with Conditionals and Loops, which will be covered in detail later. There are also rules for variables in Python: The name must start with a letter or an underscore: Both hello and _world are valid names for a variable 10hello is not, since it starts with the number 1 After that, the name may contain letters, numbers and underscores, but only letters, numbers and underscores: h3llo_w0rld is a valid name Hello-world is not, since - is not a legal character! Variable names are case sensitive: helloworld and HelloWorld will be treated as two different variables Variables are assigned left to right: x = 10 works 10 = x will give you an error Variables can be reassigned at any time with the = operator: If you say x = 5 and then x = 10, x will be considered to be 10 until you reassign it Some words can t be used as variable names, since Python uses them for other purposes. 2

As a general rule, if the word changes color, such as print, don t use it as a variable name. There are also some common conventions for variable names in most programming languages: Constants, which are variables that never change value while the program is running, are usually written in all caps: GRAVITY = -9.8. Other variables are usually written in camelcase or with underscores. Camelcase uses all lowercase letters except when there would be a space, we skip the space, and capitalize the next letter: Player one score would be stored in playeronescore or player_one_score. These conventions make it easier for programmers to read each other's code, but they are not rules of the language. You can break them any time you want, and your code will still run; it is just a good idea to follow them to make your code readable for yourself and others. Variables can also be assigned as answers to equations: While addition, subtraction work the same way that you are likely already familiar with, there are some other different operands that it you should be familiar with: To divide, we use 18/6 rather than 18 6 or 6 18 Multiplication works the same way in Python as you are familiar with, but Python uses the * symbol. Exponents use ** ie. 2**3 = 2 3 = 8. 3

The Modulus operator, also known simply as mod, is represented as x % y. Modulus is basically a remainder operator. It gives the remainder of x / y, while dropping the quotient. 10 % 5 = 0, since 10/5 = 2 R0 11 % 5 = 1, since 11/4 = 2 R1 Addition and subtraction work as you would expect: 2+3 = 5, 4-7 = -3 Order of operations works too (good old PEMDAS), but it is a good idea to use parentheses to make things explicit; 2+3*4 is 14, but it is easier to read if you write it as 2+(3*4) Try it yourself : use variables and math to convert 98.6 degrees Fahrenheit to Celsius. The equation is C = (F 32) 9. 5 4

Exercises 1. Consider the following code snippets and give the results for each: 2. Use variables to store each of your names (first, middle, and last) and print them out in the following orders: a. First, middle, last b. Last, first, middle c. Middle, first, last 3. Print the equations for the lines created by each pair of coordinates (it may be easiest to write a program and change the variable values): a. (1, 4), (2, 6) b. (5, 10), (3, 12) c. (34.5, 65.2), (81.6, 19.1) 5