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.
Hvordan kører kurset? Jeg taler en smule I programmerer en masse Læs stoffet før undervisningen Løs opgaver under og efter undervisningen Kurset afsluttes.
Introduction to C Programming
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
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.
Python November 14, Unit 7. Python Hello world, in class.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
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
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
E0001 Computers in Engineering
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Computer Science 101 Introduction to Programming.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Python From the book “Think Python”
Computer Science 101 Introduction to Programming.
© 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.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
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.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
Expressions, Statements, Variables, Assignments, Types CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
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.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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.
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
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
Variables, Expressions, and IO
First Programs CSE 1310 – Introduction to Computers and Programming
Introduction to C++ Programming
How to Run a Java Program
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
PYTHON - VARIABLES AND OPERATORS
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 (default, Apr , 23:24:47) [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 (default, Apr , 23:24:47) [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 (default, Apr , 23:24:47) [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 (default, Apr , 23:24:47) [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.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: # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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 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). Enter the radius of your circle: 21

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

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 = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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 24

Understanding the Program # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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. 25

Understanding the Program # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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: raw_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. 26

Understanding the Program # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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. 27

Understanding the Program # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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. 28

Understanding the Program # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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. 29

Modifications # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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 = “? 30

Modifications # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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 = “? 31

Changing Variable Names # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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? 32

Changing Variable Names # get the radius from the user as a string radius_text = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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 33

The Importance of Syntax # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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. 34

Syntax in this Program # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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 raw_input function Quotes and commas when we use the print function 35

The Importance of Style Original program: # get the radius from the user as a string radius_string = raw_input("Enter the radius of your circle: ") # convert the radius string to an integer. radius = int(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 36

The Importance of Style Alternative version of the same program: a = raw_input("Enter the radius of your circle: ") b = int(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? 37

The Importance of Style Alternative version of the same program: a = raw_input("Enter the radius of your circle: ") b = int(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. 38

The Importance of Style Alternative version of the same program: a = raw_input("Enter the radius of your circle: ") b = int(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 39

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. 40

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. 41

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