Introduction to Python

Slides:



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

Types and Arithmetic Operators
Lecture 2 Introduction to C Programming
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
 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
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Recitation 1 Programming for Engineers in Python.
COMPUTER SCIENCE I C++ INTRODUCTION
Chapter 1 Beginnings.
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python Programming Fundamentals
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
An Introduction to Textual Programming
Topics Introduction Hardware and Software How Computers Store Data
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
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
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
Python Let’s get started!.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
CS 100 Introduction to Computing Seminar September 12/14, 2016.
Bill Tucker Austin Community College COSC 1315
Thinking about programming
Fundamentals of Programming I Overview of Programming
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2 Introduction to C++ Programming
Introduction to Python
Variables, Expressions, and IO
Thinking about programming
Introduction to C++ Programming
Topics Introduction Hardware and Software How Computers Store Data
T. Jumana Abu Shmais – AOU - Riyadh
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Class 2.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
General Computer Science for Engineers CISC 106 Lecture 03
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Introduction to Python Dr. José M. Reyes Álamo

Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set of instructions that solve a problem and can be executed on a computer Rule 3: The best way to improve your programming and problem solving skills is to practice.

A program is a sequence of instructions. To run a program is to: Computer Programs A program is a sequence of instructions. To run a program is to: Create the sequence of instructions according to your design and the language rules Turn that program into the binary code that the computer understands Give the binary code to the Operating System (OS), so it can pass it to the processor for execution OS and processor together, execute the program

Code Example The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Aaddison-Wesley. All rights reserved

Python is an interpreted language Interpreted means that Python looks at each instruction, one at a time, and translate it into machine language. That allows you to enter instructions one-at-a-time. You can also create a file with several instructions called a script, load it, and execute all the instruction one after the other. To rerun an script, reload it.

Interactive mode vs scripting mode

Two ways to work: Interactive and scripting One of the benefits of working with an interpreted language is that you can test bits of code in interactive mode before you put them in a script. But there are differences between interactive mode and script mode that can be confusing.

For example, if you are using Python as a calculator, you might type Interactive mode For example, if you are using Python as a calculator, you might type >>> miles = 26.2 >>> miles * 1.61 42.182

Scripting mode But if you type the same code into a script and run it, you get no output at all. In script mode an expression, all by itself, has no visible effect. Python actually evaluates the expression, but it doesn’t display the value unless you tell it to do so: miles = 26.2 print miles * 1.61

Calculating area and circunference

Code Example The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Aaddison-Wesley. All rights reserved

One thing we did was to import the math module with import math Import of Math One thing we did was to import the math module with import math This imported python math statements We precede all operations of math with math.OPERATION e.g. math.pi, math.pow(x, y)

Getting Input The function: input(“Give me a value”) Prints “Give me a value” on the python screen and waits until the user types anything and presses Enter After pressing enter, the result is a string even for numbers

Assignment Statements The ‘=‘ is the assignment statement The value to the right is associated with the variable name on the left It does not stand for equality!

Convert from string to integer To do math, Python requires converting the sequence of characters into an integer

Printing Output myVar = 12 print (‘My var has a value of:’,myVar) print takes a list of elements to print, separated by commas If the element is a string, prints it as is If the element is a variable, prints the value associated with the variable After printing, moves on to a new line of output

Save as a script When you save a file, such as our first program, and place a .py suffix on it, it becomes a python module or script You “run” the module from the IDLE menu to see the results of the operation A module is just a file with a bunch of python commands Python provides a lot modules for common tasks such as math, databases, networking, etc.

Errors!!! If Python interpreter does not understand your code you will get an error (or errors) Check the syntax and fix it You can them import the program again until there are no more errors

Common Error When using IDLE, if you save a file without a .py extension, it will not colorize and format the file. Resave with the .py extension

Formal Elements of Programming These apply to any computer programming language

Statements Statements are commands. n = n + n ** 3 n = 3 They perform some action n = n + n ** 3 n = 3 print ‘Hello!’

Expressions Expressions perform some operation and have a value 3 Typically do not modify values in the interpreter 3 n + 3 n ** 3

Comments Code is what actually executes Comments do not execute but they make the programs easier to understand, improving readability. n + 3 #this an expression n = n ** 3 #this is an statement print ‘Hello!’ #this is another statement

Literals Literal is a programming notation for a fixed value. Integer numbers are literals because they have fixed values e.g. 123, 999, 1000.

Operators Integer Floating point addition and subtraction: +, - multiplication: * Division (quotient): / remainder: % Floating point add, subtract, multiply, divide: +, -, *, /

Variables A variable is a location in memory where we store data in our program. We assign names to variables to make our program more readable.

Variables Name Value X 7 X = 7 Python maintains a list of pairs for every variable: variable’s name variable’s value A variable is created when a value is assigned the first time. It associates a name and a value Subsequent assignments update the associated value. We say the variable name references the value A variable type depends on the value assigned to it. Name Value X 7 X = 7

Python Naming Conventions Must begin with a letter or _ Ab123 is OK, but 123ABC is not. May contain letters, digits, and underscores this_is_an_identifier_123 May be of any length Upper and lower case letters are different LengthOfRope is not the same as lengthofrope Names starting with _ have special meaning.

When = Doesn’t Mean Equal It is most confusing at first to see the following kind of expression: myInt = myInt + 7 What’s looks strange here is that the = doesn’t mean equal, but assignment

= sign means assignment In many computer languages, = means assignment. myInt = myInt + 7 lhs = rhs What assignment means is: Evaluate the expression on the right-hand-size of the equal sign Take the resulting value and store it in the variable indicated on the left-hand-size Only variables go into the left-hand side of an assignment statement

More on Assignment Example: x = 3 * 5 + 2 evaluate expression (3*5 + 2) = 17 change the value of x to 17 Example (if y has value 2): y = y + 3 evaluate the expression (y+3): 5 change the value of y to 5

Variables and Types Python does not require you to pre-define the data type of a variable The data type of a variable can change Nonetheless, knowing the data type can be important for using the correct operation on a variable. Thus proper naming is important!

What is a Type? A Python data type essentially defines two things: The internal structure of the type (what it contains) The operations you can perform on it. You can capitalize letter, and add numbers You cannot capitalize numbers or add letter.

Python Data Types Integer: 5 Float: 1.2 Boolean: True, False String: “anything” or ‘something’ List: [,]: [‘a’,1,1.3] Others

Fundamental Types Integers Floating Point 1, -27 Floating Point 3.14, 10.0, .001, 3.14e-10, 0e0 Boolean (True or False values) True, False (notice the use of caps)

Converting Types A character ‘1’ is NOT the same as the integer 1 You need to convert the value returned by the input command (characters) into an integer int(“123”) yields the integer 123

Type Conversion int(someVar) converts to an integer float(someVar) converts to a float str(someVar) converts to a string should check out what works: int(2.1)  2, int(‘2’)  2, but int(‘2.1’) fails float(2)  2.0, float(‘2.0’)  2.0, float(‘2’)  2.0, float(2.0)  2.0 str(2)  ‘2’, str(2.0)  ‘2.0’, str(‘a’)  ‘a’

Types and Division Python does binary operations on two values of the same type, yielding a value of that type: 2/3, integer types, yield integer (0). 2%3 is the remainder, an integer (2) 2.0/3.0, float types, yield float (0.66666)

Mixed Types 4/3 results is 1 under integer division 4.0/3.0 results in 1.3333333 under float division What is the result of 4/3.0? Python will automatically convert to the most detailed type. Thus 4  4.0, and the result is 1.3333333

LAB2_Python1