Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Python

Similar presentations


Presentation on theme: "Introduction to Python"— Presentation transcript:

1 Introduction to Python
Introduction to Computing Science and Programming I

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

3 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

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

5 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

6 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 ”

7 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

8 Data Type You need to be careful
“123” is a string, 123 is an integer, 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 … The ‘.0’ makes the first operand of type float and therefore the result is given as a float

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

10 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”)

11 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

12 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

13 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

14 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

15 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( ,2) returns 12.34 round( ,3) returns

16 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( ) is 14.0

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

18 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

19 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

20 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


Download ppt "Introduction to Python"

Similar presentations


Ads by Google