Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in...

25
Python Let’s get started!

Transcript of Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in...

Page 1: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

PythonLet’s get started!

Page 2: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

Getting Started

• Two modes: • Interactive

• Script ** we will be working in script mode for the most part

Page 3: 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

Page 4: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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 ( )

Page 5: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 6: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 7: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 8: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

Delimiters

• Python recognizes three types of delimiters• The single “tick” : ( ‘ hello ’ )

• The single “quote” : ( “ hey there! ” )

• The triple quote : ( “”” what’s up dude “”” )

Page 9: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

Print Challenge

• 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!

Page 10: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 11: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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)

Page 12: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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)

Page 13: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 14: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 15: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 16: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 17: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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)

Page 18: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 19: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

Examples

class = 2

Class = 2

classAvg = 24

_class_avg = 42

2ndclassAvg = 56

classavg! = 99

Page 20: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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 “_” )

Page 21: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 22: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 23: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 24: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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

Page 25: Python Let’s get started!. Getting Started Two modes: Interactive Script ** we will be working in script mode for the most part.

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