A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Introduction to C Programming
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
CS 201 Functions Debzani Deb.
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Introduction to C Programming
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Computer Science 101 Introduction to Programming.
An Introduction to Textual Programming
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Introduction to Python
Fundamentals of Python: First Programs
General Programming Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Science 101 Introduction to Programming.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python From the book “Think Python”
Computer Science 101 Introduction to Programming.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Variables, Types, Expressions Intro2CS – week 1b 1.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
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.
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.
Fundamentals of Programming I Overview of Programming
Introduction to Computing Science and Programming I
Chapter Topics The Basics of a C++ Program Data Types
Whatcha doin'? Aims: To start using Python. To understand loops.
A Playful Introduction to Programming by Jason R. Briggs
Introduction to Python
Completing the Problem-Solving Process
Basic Elements of C++.
Variables, Expressions, and IO
First Programs CSE 1310 – Introduction to Computers and Programming
Basic Elements of C++ Chapter 2.
Learning to Program in Python
Introduction to C++ Programming
12th Computer Science – Unit 5
Unit 3: Variables in Java
First Programs CSE 1310 – Introduction to Computers and Programming
How to Run a Java Program
Presentation transcript:

A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of this material has been created by Dr. Darin Brezeale and Dr. Gian Luca Mariottini 1

Simplest Code: Numerical Calculations Start the Python shell. You see a welcoming message and the command prompt. Python (v3.3.2:d047928ae3f6, May , 00:06:53) [MSC v bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> Terminology: we will call >>> “the command prompt”. This is Python’s way of telling you “I am waiting for your input”. 2

Simplest Code: Numerical Calculations Let’s type in a single number, and press ENTER. Python (v3.3.2:d047928ae3f6, May , 00:06:53) [MSC v bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> 14 3

Simplest Code: Numerical Calculations After we press ENTER, the computer evaluates what we just typed, and prints the result. Python (v3.3.2:d047928ae3f6, May , 00:06:53) [MSC v bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> >>> 4

Simplest Code: Numerical Calculations After we press ENTER, the computer evaluates what we just typed, and prints the result. Python (v3.3.2:d047928ae3f6, May , 00:06:53) [MSC v bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> >>> This is not very exciting yet, the computer did not tell us anything we did not know. 5

Python as a Calculator. >>> (23*3) + 12/ We can type in arbitrary numerical expressions, and Python evaluates them. This is still not that exciting. However, such calculations are a useful building block for real programs. 6

Operators Here is a list of operators used in Python: + - * ** / // % > & | ^ ~ = == != <> += -= *= /= //= %= &= |= ^= >>= <<= **= Do not try to memorize them, but learn to look them up in the book as needed (chapter 1). – Try them out to verify you understand what they do. 7

Some Operators >>> 2** ** is the “exponentiation” operator >>> 13%4 1 % is the “remainder” operator 8

Order of Operators What does this do? >>> 2**20/2 Do we first do 2**20 and then divide by 2, or do we first do 20/2 and then 2**10? 9

Order of Operators What does this do? >>> 2**20/2 Do we first do 2**20 and then divide by 2, or do we first do 20/2 and then 2**10? The book defines the order of operators in chapter 1. Suggestion: USE THESE RULES MINIMALLY. 10

Order of Operators Instead of >>> 2**20/2 You should type >>> (2**20)/2 or >>> 2**(20/2) 11

Circumference and Area of Circle Computing the circumference of a circle with radius = : – Circumference = radius * pi * 2 >>> * * Computing the area of the same circle: – area = (radius ** 2) * pi >>> ( ** 2) *

Using Variables >>> * * 2 >>> ( ** 2) * Tedious to type in long numbers repeatedly. The above lines are hard to read. Instead, we can do: >>> radius = >>> pi = >>> circumference = radius * pi * 2 >>> area = (radius ** 2) * pi 13

Using Variables When we type in these four lines, Python prints nothing back. >>> radius = >>> pi = >>> circumference = radius * pi * 2 >>> area = (radius ** 2) * pi How can we see the actual results? 14

Using Variables When we type in these four lines, Python prints nothing back. >>> radius = >>> pi = >>> circumference = radius * pi * 2 >>> area = (radius ** 2) * pi How can we see the actual results? >>> circumference >>> area

Doing Repeated Calculations What if we want to calculate the area and circumference of circles many times per day (or many times per hour)? We can just type in the formulas (as we did in the previous slides) again and again. – Any shortcomings of that approach? 16

The Need for a Program What if we want to calculate the area and circumference of circles many times per day (or many times per hour)? Typing in the formulas again and again is tedious, and error prone. Here is where we can use our first PROGRAM. 17

Creating a Program Create a text file, called “circles_1.py”. Easy way, from Python shell: – File -> New Window Creates a new text window – File -> Save Allows you to save the file using a name of your choice. IMPORTANT: Make sure you understand what a folder is, and that you know where your file is saved. Talk to the class TA to learn how to do that. 18

Creating a Program Within the file, we put in this text: # specify the radius value radius = # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) 19

Running the program From the text file window, choose Run -> Run Module (or simply press F5). 20

Running the program From the text file window, choose Run -> Run Module (or simply press F5). Circumference = area =

Problem: Radius is Hardcoded Why is this a problem? 22

Problem: Radius is Hardcoded Why is this a problem? Biggest reason: the user needs to be a programmer. – You cannot use this program without changing the program. 23

Solution Allow the user to enter the radius value as input. 24

Revised Program # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) 25

Running the program From the text file window, choose Run -> Run Module (or simply press F5). Enter the radius of your circle: 26

Running the program From the text file window, choose Run -> Run Module (or simply press F5). Enter the radius of your circle: 2 27

Running the program From the text file window, choose Run -> Run Module (or simply press F5). Enter the radius of your circle: 2 Circumference = area =

Understanding the Program # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) 29

Understanding the Program # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) Comment lines: Are notes to ourselves or other people, the computer ignores them. 30

Understanding the Program # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) Getting user input: input is a PREDEFINED function in Python. Its job is to print out a message, receive input from the user, and store that input into a string. 31

Understanding the Program # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) Type conversion: radius_string is a string, meaning that it is a variable that stores text. Instead, we are interested in the contents of radius_string as a number. The int function is a PREDEFINED Python function, its job is to convert a string into a number. 32

Understanding the Program # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) Assignments: These lines perform numerical calculations, and store the results of those calculations in variables. 33

Understanding the Program # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) Printing results: These lines print out results. print is a predefined Python function. It prints out strings that we text, as well as values of variables. Note separation by comma. 34

Modifications # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) How would you modify this program to print “The area of the circle is “ instead of “area = “? 35

Modifications # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print(“The area of the circle is ", area) How would you modify this program to print “The area of the circle is “ instead of “area = “? 36

Changing Variable Names # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) What if I want to change the name of variable radius_string to radius_text? 37

Changing Variable Names # get the radius from the user as a string radius_text = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_text) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) What if I want to change the name of variable radius_string to radius_text? I have to simply replace all occurrences of radius_string with radius_text 38

The Importance of Syntax # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) Python (like all programming languages) is very picky about syntax. A single misplaced character can make a program not work. Note the syntax used in this program, and make sure you use the SAME syntax in your code. 39

Syntax in this Program # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) # in the beginning of comment lines. parentheses and quotes when we use the input function Quotes and commas when we use the print function 40

The Importance of Style Original program: # get the radius from the user as a string radius_string = input("Enter the radius of your circle: ") # convert the radius string to a real number. radius = float(radius_string) # compute and print the circumference pi = circumference = radius * 2 * pi print("Circumference = ", circumference) # compute and print the area area = (radius ** 2) * pi print("area = ", area) 41

The Importance of Style Alternative version of the same program: a = input("Enter the radius of your circle: ") b = float(a) c = d = b * 2 * c print("Circumference = ", d) e = b ** 2 * c print("area = ", d) Both versions will run EXACTLY the same. What makes the previous version preferable? 42

The Importance of Style Alternative version of the same program: a = input("Enter the radius of your circle: ") b = float(a) c = d = b * 2 * c print("Circumference = ", d) e = b ** 2 * c print("area = ", d) Both versions will run EXACTLY the same. What makes the previous version preferable? Readability. Makes code easier to verify and correct. 43

The Importance of Style Alternative version of the same program: a = input("Enter the radius of your circle: ") b = float(a) c = d = b * 2 * c print("Circumference = ", d) e = b ** 2 * c print("area = ", d) Specific differences: – Lack of comments – Non-descriptive variable names – Lack of empty lines to separate “blocks” of code 44

Some Guidelines To learn how to code, you need PRACTICE. – What will usually not work: Listen to the lectures. Go and try to do the assignments. – What will usually work: Listen to the lectures and KEEP NOTES. Actually run every piece of code that we do in class. Understand every line of every piece of code we do in class. Think of variations of what we do in class, and try them. – Predict what the variation will do, and verify by running it. Then try the assignments. 45

Some Guidelines You need to understand the terminology: – Statements, expressions, tokens, literals, functions, strings, variables, operators, … You will encounter many terms in this course. YOU NEED TO LEARN EXACTLY WHAT THEY MEAN. DO NOT RELY ON ENGLISH. These terms have meanings in conversational English that are only vaguely related with their meaning in programming. 46

Terms We Have Seen So Far: Command prompt Text file Filename Folder Operator Variable Function Running a program 47