Python Let’s get started!.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Introduction to C Programming
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to Python
Chapter 2 Writing Simple Programs
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
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
* Note: All of the material for the Required Textbook, Optional Textbook are protected by Copyright Law. * Professor Sana Odeh’s notes and programs listed.
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.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Python.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
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.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Introduction to Programming with RAPTOR
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
CS346 Javascript -3 Module 3 JavaScript Variables.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Variables, Expressions, and Statements
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
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.
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introducing Python 3 Introduction to Python. Introduction to Python L1 Introducing Python 3 Learning Objectives Know what Python is and some of the applications.
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.
Chapter 2 Writing Simple Programs
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Introduction to Python
Formatting Output.
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Formatting Output.
Review.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Lecture 2 Python Programming & Data Types
“If you can’t write it down in English, you can’t code it.”
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Lecture 2 Python Programming & Data Types
Variables, Data Types & Math
15-110: Principles of Computing
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Python Reserved Words Poster
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Python Let’s get started!

Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part

Running a program Open IDLE File  New Window File  Save (add “.py” to your file, it may not add it automatically) Run  Run Module

Functions A “function” is a pre-written code that will perform a specific task Python comes with a number of pre-written functions, but you can also write your own (as we will later on in this class) Functions begin with a keyword and are followed by a set of parentheses Ex: print ( )

Functions Functions can pass what we call “arguments”, which are placed inside the parentheses of the function Ex: print (“Hello, world!”) Different functions will expect different types of arguments (more on types of arguments later) When you use a function on python, we say we are “calling” a function

Types of Data There are three types of data that we will use in Python for our purposes Strings: textual data Integers: numerical data, restricted to integers Floats: numerical data, decimals included

Strings A string is data that is textual in nature Strings can contain none or some characters “String literals” are strings defined within your program/function and must be “delimited” by a special character which tells Python to treat it as textual data, and not as a function Ex: print (“Hello, world!”) Ex: print (“print”)

Delimiters Python recognizes three types of delimiters The single “tick” : ( ‘ hello ’ ) The single “quote” : ( “ hey there! ” ) The triple quote : ( “”” what’s up dude “”” )

Print Challenge Hello, my name is Bruce! Hi, Bruce! Try to write a Python program that prints the following lines of text: Hello, my name is Bruce! Hi, Bruce! Fish are friends, not food!

Print Function You’ll notice that the print function will automatically add a line break after it prints your argument “”” the triple quote delimiters can be used for data that is on multiple lines

Printing multiple arguments The print function can accept none or multiple arguments at a time Multiple arguments can be separated by a comma Example: print (“Hello!”, ‘my name is’, “Donald”) The print function will automatically insert a space between any two arguments (we will learn how to avoid this later on)

Commenting your program As we mentioned, your programs can become very long, so you will want to write comments either for yourself or another user Python recognizes the symbol # as a commentator symbol, so it will ignore everything that comes after that symbol on the same line # First, I will ask the user for their name, I will then store it in a variable called “x” x = input (“What is your name?”) # Now, I will print the name of the user print (x)

Multi-line comments You can also input multi-line comments on Python using the triple quote delimiter “”” Python will ignore this line And this line And this one too print (“Hello, world!”)

Comments as Pseudocode Comments are often used as pseudocode in order to outline the task at hand Ex: #get two test scores from user # add test scores # divide by 2 # print out result

Comments as a debugging technique Comments can be used for debugging because Python will ignore portions of your code Debugging means to identify and to remove any errors in your code # I don’t know why this one isn’t working! # print ( ‘ Hello, world! ” )

Variables Variables are like buckets that hold and store information in your computer’s memory You can create a variable by using the following syntax: variablename = data Examples: speed = 5 name = “Donald” The “=” is the assignment operator and it tells Python to store data on right of operator into variable, named on the left of the operator

Rules on naming variables You cannot use reserved words (they are listed on the next slide) Variable names can’t contain spaces but they can contain an underscore “_” The first character of a variable name must be a letter or an underscore, it cannot be a numerical value Python is case sensitive (variablename vs. VariableName)

Reserved words These words cannot be used as variable names: False, True, None, and, as, assert, break, class, continued, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield (you’ll notice, True, False, None have capital letters) Reserved words will show up in a different color while writing in script mode

Examples class = 2 Class = 2 classAvg = 24 _class_avg = 42 2ndclassAvg = 56 classavg! = 99

Examples class = 2 illegal (reserved word) Class = 2 legal classAvg = 24 legal _class_avg = 42 legal 2ndclassAvg = 56 illegal (can’t start with a number) classavg! = 99 illegal (only alphanumeric values, and “_” )

Common Variable Naming Techniques Some variable names can be hard to read, so there are common techniques that are used to distinguish them: cartopspeed = 140 # hard to read … car_top_speed = 140 carTopSpeed = 140 car_top_speed_3 = 140

Printing variables You can print the data that is stored within a variable by passing the variable as an argument into the print function name = “Donald” name = Donald print (name) print (“Hello, my name is”, name) >> Donald >> Hello, my name is Donald

Changing variables Variables are called variables because the data stored in them can be changed You can change the value/data stored in a variable with a second assigning statement dollars = 19.99 print (“I have”, dollars, “in my account”) dollars = 10000.99 print (“Now, I have”, dollars, “in my account”)

Multiple Assignments You can assign multiple variables on the same line: x, y, z = 1, 2, 3 # the variables will assume the values in order You can also assign the same value to multiple variables x = y = z = 10 # variables x, y, and z will all hold the value of 10

Practice You’re working on a simple inventory management system for a small store. You’d like to store the name and price of two products and print them, so that your inventory page looks something like this: Item: Bread, price: $ 1.99 Item: Eggs, price: $ 3.49