CMSC 201 Computer Science I for Majors

Similar documents
Programming with Python

CMSC201 Computer Science I for Majors

Variable and Data Type I

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

Variable and Data Type I

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Variables, expressions and statements

CMSC201 Computer Science I for Majors

ENGR 101 Engineering Design Workshop

Chapter 2 Writing Simple Programs

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

PROGRAMMING FUNDAMENTALS

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

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

CMSC201 Computer Science I for Majors

Text Input and Conditionals

CMSC201 Computer Science I for Majors

CMSC 201 Spring 2018 Lab 01 Hello World

Intro. Scheme Basics. scm> 5 5. scm>

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Lecture 3. Input, Output and Data Types

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

VARIABLES & ASSIGNMENTS

CMSC 201 Fall 2018 Lab 04 While Loops

Algorithms and Programming I. Lecture#12 Spring 2015

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Fundamentals of Programming CS-110. Lecture 2

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Basic Syntax - First Program 1

Python Unit

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CMSC 201 Spring 2017 Lab 01 Hello World

CS1 Lecture 3 Jan. 18, 2019

CMSC 201 Spring 2017 Lab 12 Recursion

T H E I N T E R A C T I V E S H E L L

About Variables in Python F E B 1 1 T H

Fall 2017 CISC124 9/16/2017

Computer Science 217

CMSC201 Computer Science I for Majors

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Introduction to Computer Programming for Non-Majors

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

Introduction to Python (All the Basic Stuff)

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Fundamentals of Programming (Python) Getting Started with Programming

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Getting started 7. Saving data 23

Full file at

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

cs1114 REVIEW of details test closed laptop period

CS1 Lecture 3 Jan. 22, 2018

Invent Your Own Computer Games with Python

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Introduction to Problem Solving and Programming in Python.

Procedures, Parameters, Values and Variables. Steven R. Bagley

CSCE 110 Programming I

ME30 Lab3 Decisions. February 20, 2019

These are notes for the third lecture; if statements and loops.

Play with Python: An intro to Data Science

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Getting Started with Python

CMSC 201 Spring 2018

Jython. secondary. memory

Flow Control: Branches and loops

CS 115 Lecture 4. More Python; testing software. Neil Moore

Introduction to Programming EC-105. Lecture 2

Variables, Expressions, and Statements

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

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

CS 1110, LAB 1: PYTHON EXPRESSIONS.

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Computer Science 217

CMSC 201 Spring 2017 Lab 05 Lists

CMSC 201 Fall 2016 Lab 13 More Recursion

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

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

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

Lesson 2A Data. Data Types, Variables, Constants, Naming Rules, Limits. A Lesson in Java Programming

Control Structures 1 / 17

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Chapter 17. Fundamental Concepts Expressed in JavaScript

Transcription:

CMSC 201 Computer Science I for Majors Lecture 02 Intro to Python

Syllabus Last Class We Covered Grading scheme Academic Integrity Policy (Collaboration Policy) Getting Help Office hours Programming Mindset Failure (isn t really failure) 2

Any Questions from Last Time? 3

Today s Objectives To start learning Python To learn about variables How to use them Different types To learn how to use input and output To do interesting things with our program Written programs vs Python interpreter 4

Variables 5

Python Python is a widely used language General purpose High-level language Emphasizes code readability More streamlined than some other languages 6

Hello World! In Python: print("hello World!") In the C++ programming language: #include <iostream> int main() { } std::cout << "Hello World!\n"; 7

Identifiers Elements of a Program Variables Functions (later in the semester) Expressions Code that manipulates or evaluates identifiers 8 Literals Operators

What Is a Variable? Something that holds a value Can change (unlimited number of times) Similar to variables in math In simple terms, a variable is a box that you can put stuff in 9

Rules for Naming Variables Variable names can contain: Uppercase letters (A-Z) Lowercase letters (a-z) Numbers (0-9) Underscores (_) Variables can t contain: Special characters like $, #, &, ^, ), (, @ 10

More Rules for Naming Variables Variables can be any length x IsKanyeRunningForPresidentIn2020 myname Variables cannot start with a digit 2cool4school is not a valid variable cool4school is a valid variable 11

Variables and Keywords Keywords are reserved words in Python False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise Variables cannot be keywords or is not a valid variable name orange is an acceptable variable name 12

Exercise: Variables Are the following legal or illegal in Python? 1spam raise1 Spam_and_Eggs EXIT_CODE 13

Exercise: Variables Are the following legal or illegal in Python? 1spam raise1 Spam_and_Eggs EXIT_CODE No Illegal! Yes legal! Yes legal! Yes legal! 14

Exercise: Variables Are the following legal or illegal in Python? 15 Spam_and_Eggs Yes legal! But it doesn t follow our coding standards! spamandeggs or spam_and_eggs

Using Variables in Python You create a variable as soon as you declare it You also need to initialize it before using it Use the assignment operator (equal sign) mascotumbc = "dog" newstudents = 1772 dogsaregood = True assignment operator 16

Expressions 17

Expressions Programs manipulate data Allows us to do interesting things Expressions calculate new data values Use assignment operator to set new value 18

Expressions Example variable being set numcandy = 4 pricecandy = 0.58 totalcost assignment operator value (a literal ) = numcandy * pricecandy expression 19

Common Mistake Many new programmers mix up the left and right hand sides of the assignment operator Variable being set must be on the left Expression is on the right Evaluate the expression first, then assign the value numcandy = 4 + 1 4 + 1 = numcandy 20

Variable Types There are many different kinds of variables! Numbers Whole numbers (Integers) Decimals (Floats) Booleans (True and False) Strings (collections of characters) 21

Variables Types: Examples astring = "Hello class" float_1 = 1.12 mybool = True aninteger = 7 dogname classcode = 201 = "Ms. Wuffington" 22

Variable Usage Variables are designed for storing information Any piece of information your program uses or records must be stored in a variable Python doesn t have a short term memory, so everything needs to be written down for it 23

Literals and Operators 24

Literals Literals in Python are values you use literally Can be assigned to a variable or not For example: 2 is an integer literal Hello is a string literal 4.0 is a float literal False is a Boolean literal 25

Using Literals The expression below assigns the string literal CMSC to a variable called major major = "CMSC" The expression below prints the integer literal 50 without assigning it to a variable print(50) 26

Operators Operators are special symbols that allow Python to perform different operations There are many types of operators Mathematical Comparison Assignment Logical 27

Operator Types We won t cover all the types in detail today, but here are some simple examples 28 Mathematical + - * / % Comparison < <=!= >= == Assignment = += *= we ll cover the weird ones later

Practice Exercises Print the value of the variable mydog Remember to assign a value to mydog first! Set a value for a variable called bill, and calculate and print the 15% tip for that bill Create your own expression using at least two variables, and print out the result 29

Input and Output 30

Output Output is text that is printed to the screen So the user can see it The command for this is print Use the keyword print and put what you want to be displayed in parentheses after it 31

Output Example print (3 + 4) print (3, 4, "3 + 4", 3 + 4) print() print("the answer is", 3 + 4) 7 3 4 3 + 4 7 What does this output to the screen? The answer is 7 32

Output Exercise 1 What will the following code snippet print? a = 10 b = a * 5 c = "Your result is:" print(c, b) Your result is: 50 33

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

Output Exercise 2 Explanation Why does it print out 10? When you set one variable equal to another, they don t become linked! They are separate copies of a value After b is set to 10, it no longer has anything else to do with a 35

Output Exercise 2 Explanation a = 10 b = a a = 3 print(b) 10 36

Output Exercise 2 Explanation a = 10 b = a a = 3 print(b) 10 10 37

Output Exercise 2 Explanation a = 10 b = a a = 3 print(b) 103 10 38

Output Exercise 2 Explanation a = 10 b = a a = 3 print(b) output: 10 3 10 39

Input Input is information we get from the user We must tell them what we want first usernum = input("please enter a number: ") print(usernum) The input and output will look like this: Please enter a number: 22 22 40

How Input Works usernum = input("please enter a number: ") Takes the text the user entered and stores it In the variable named usernum You can do this as many times as you like! usernum = input("enter another number: ") usernum2 = input("enter a new number: ") userage = input("please enter your age: ") 41

Input as a String Everything that is stored via input() will come through in the form of a string There is a difference between "10" and 10 "10" is a string containing two characters 10 is understood by Python as a number 42

Converting from String To turn an input string into a number, you can do the following: anum = input("enter a number: ") anum = int(anum) int stands for integer (a whole number) You can also do it in one line: anum = int(input("enter a number: ")) 43

Converting from String We can cast to other data types as well gpa = float(input("enter GPA: ")) Do you think the string "1,024" will work if we try to cast it as an integer? Why? It won t work The comma character isn t a number 44

Written Programs vs Python Interpreter 45

We Started Python Today! Two ways to use Python We will write programs for assignments You can write a program as a series of instructions in a file and then execute it Use the interpreter to help you test things You can also test simple Python commands in the Python interpreter 46

Written Programs Create, write, and save a Python file (.py) File is run via the command line python3 myprogram.py File must be complete to run correctly Program cannot be edited on the fly Must be exited, file re-opened, changes made, file saved and closed, and then re-run the program 47

Python Interpreter The interactive interpreter evaluates each individual line of code as it s typed in Type python3 to launch the interpreter 48 >>> print("hello") >>> is where the user types their code Hello lines without a >>> are Python s response >>> 4 + 7 11 >>>

Time For 49

CTRL+X, CTRL+S Saves the file and stays in emacs Allows you to keep editing the file CTRL+X, CTRL+C Closes emacs, does not automatically save the file Will prompt you to save if changes were made 50

Announcements Your discussions (Labs) start next week! Go to your scheduled location and time Pre Lab quiz will be posted and announced on BB Lab 1 is due Sunday, Sept 9th at 8:59:59 PM In-person labs start the week of September 10th HW0 is also due on Sept 9th at 8:59:59 PM 51

Image Sources Cardboard box: https://pixabay.com/p-220256/ No cursing sign (adapted from): https://www.flickr.com/photos/rtgregory/1332596877 Rock candy: https://commons.wikimedia.org/wiki/file:rock-candy-sticks.jpg Broken chain: https://pixabay.com/p-297842/ 52