Introduction to Python

Slides:



Advertisements
Similar presentations
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Advertisements

Python November 14, Unit 7. Python Hello world, in class.
Chapter 2 Writing Simple Programs
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Python.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Introduction to Python
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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.
Fundamental Programming: Fundamental Programming Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Variables, Expressions, and Statements
Variables, Expressions and Statements
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.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Python Let’s get started!.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Chapter 2 Writing Simple Programs
C++ First Steps.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Literals and Variables Chapter 2, Sections
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
CS1022 Computer Programming & Principles
Python Let’s get started!.
Formatting Output.
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Introduction to Scripting
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Formatting Output.
Functions CIS 40 – Introduction to Programming in Python
Variables, Expressions, and Statements
Few More Math Operators
Topics Introduction to File Input and Output
Introduction to C++ Programming
“If you can’t write it down in English, you can’t code it.”
Reading Input from the Keyboard
Variables, Expressions, and Statements
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Variables, Data Types & Math
Variables, Expressions, and Statements
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Unit 3: Variables in Java
Topics Introduction to File Input and Output
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Python Introduction to Computing Science and Programming I

Literals Literals These are values in the code that represent themselves 1, 22.8, -45, “Chris”, ‘-356’

The print Command Use print to output a line of information to the screen print “Hello World” To print multiple items separated by single spaces use commas. The output for this command is identical to the one above print “Hello”,”World” The command will also output numbers

Calculations An expression is any kind of calculation that produces a result 34 + 12 7 – 3 “Combine two “ + “strings”

Calculations Basic Math +,-,*,/ work as you’d expect. Python follows the normal order of operations 5+2*2 is 9 Parentheses () also work as they do in mathematics (5+2)*2 is 14

Calculations String “math” To combine (concatenate) two strings use + “Chris “ + “Schmidt” is “Chris Schmidt” You’ll be using this quite a bit for output to the screen If you want to repeat a string, use * “Chris ” * 3 is “Chris Chris Chris ”

Data Types Different types of information have different data types. Python will behave differently depending on what type of information it is working with Data Types Integers 9, 34, -5 Floats 23.45, 12.0, 15.3 Strings “Chris”, “This is a string” We’ll see other data types in the future

Data Type You need to be careful “123” is a string, 123 is an integer, 123.0 is a float In mathematical operations if both operands are integers, the result is given as an integer 5 / 3 is 1 Notice that the result has the decimal portion removed instead of being rounded 5.0 / 3 is 1.66666… The ‘.0’ makes the first operand of type float and therefore the result is given as a float

The type Function Python provides a function to tell you what data type a piece of information is type(24) returns <type ‘int’>

Type Conversion It is very common to need to convert one data type to another. Python provides simple operations to do so. To convert use the int, float, and str functions int(6.8) returns 6, int(“123”) returns 123 float(6) returns 6.0, float(“34.6”) returns 34.6 str(123) returns “123”, str(4.5) returns “4.5” If the conversion isn’t possible, there will be an error. E.g. int(“narf!”), int(“one”)

Variables Variables To store a value use a variable to reserve a small piece of the computer’s memory An assignment statement is used to store a value in a variable name = “Chris” id = 123 height = 1.7 If you want to use the value stored in the variable, simply use the variable’s name print name

Variables Allowed names Variable name start with a letter and can contain letters, numbers, and underscores id, student_name, number6 are examples of allowed variable names Python keywords that are part of the programming language cannot be used as a variable name. E.g. print and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try Python is Case-Sensitive so ‘counter’ and ‘cOUNteR’ will not access the same variable

Variables Point of good coding: Give your variables intelligent descriptive names BAD: n=“Chris” d=5 GOOD: name=“Chris” numberOfDays=5 This makes it easier for anyone, including you, to understand what is going on in the code

Functions Functions in python are similar to functions in mathematics, they take arguments as input and return a value Python has many built in functions and we will learn how to write our own functions later

Functions A couple simple built in functions len takes a string as its only argument and returns the length len(“Chris”) returns 5 len(“”) returns 0 round can take one or two arguments If the only argument is a number round returns the number rounded to the nearest integer round(22.3) returns 22.0 A second optional argument indicates how many decimal places to round to round(12.3456,2) returns 12.34 round(12.3456,3) returns 12.346

Functions Functions can be part of an expression x = 12 + round(22.6) Variables can be used as arguments for a function name=“Chris” nameLength=len(name) Any expression can be uses as an argument round(12.1+1.6) is 14.0

The raw_input Function This is a function that allows you to take input from the user. It takes one argument, a string that will be printed to the screen as a prompt to the user. The function returns the user’s input as a string.

A Bit More On Strings There are a couple things you can do with strings to help format output or display certain characters You can escape a character by using a backslash \ in a string. The backslash and the character after it combine to signify one character in the string. The start and end of a string are denoted by single or double quotation marks. Therefore you can’t just drop them in the middle of a string without using the backslash print “Descartes said “I think, therefore I am”” Python will think the string has ended when you hit the quotation mark before I print “Descartes said \”I think, therefore I am\”” This will print correctly, the \” represents the quotation mark within the string

A Bit More On Strings Other escape characters \\ to represent a backslash in a string Since the backslash is used to indicate a special character \n represents a newline “This is the first line \n This is the second line” \t represents a tab There are others, but just be aware of the idea of escape charaters

A Bit More On Strings Python provides a way to simplify formatting of a string and avoid using escape characters. If you begin and end a string with 3 quotation marks, Python will handle all of this for you and format the string as you wrote it