introduction to python n.
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to Python PowerPoint Presentation
Download Presentation
Introduction to Python

Loading in 2 Seconds...

play fullscreen
1 / 20

Introduction to Python - PowerPoint PPT Presentation

  • Uploaded on

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”

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

Introduction to Python

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to python

Introduction to Python

Introduction to Computing Science and Programming I

  • Literals
    • These are values in the code that represent themselves
      • 1, 22.8, -45, “Chris”, ‘-356’
the print command
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
  • An expression is any kind of calculation that produces a result
    • 34 + 12
    • 7 – 3
    • “Combine two “ + “strings”
  • 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
  • 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
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
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
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
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
    • 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

name = “Chris”

print name

  • 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
  • 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 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
  • 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 can be part of an expression
    • x = 12 + round(22.6)
  • Variables can be used as arguments for a function



  • Any expression can be uses as an argument
    • round(12.1+1.6) is 14.0
the raw input function
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
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 strings1
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 strings2
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