Python Lets get started Getting Started Two modes
- Slides: 26
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 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” : • The single “quote” : • The triple quote : ( ‘ hello ’ ) ( “ hey there! ” ) ( “”” what’s up dude “”” )
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!
Print Challenge • Now try these: Bruce asked, “When was the last time you had fish? ” He’s leading an FEA group. FEA stands for “Fish Eater’s Anonymous. ”
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 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. Variable. Name)
Reserved words • These words cannot be used as variable names: False, True, None, and, 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 class. Avg = 24 _class_avg = 42 2 ndclass. Avg = 56 classavg! = 99
Examples class = 2 illegal (reserved word) Class = 2 legal class. Avg = 24 legal _class_avg = 42 legal 2 ndclass. Avg = 56 illegal (can’t start with a number) classavg! = 99 and “_” ) illegal (only alphanumeric values,
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 car_top_speed = 140 car. Top. Speed = 140 car_top_speed_3 = 140 # hard to read …
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” 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
- The secret of getting ahead is getting started
- Get in get on get off get out
- Lets get started images
- Song with poetic devices
- Two modes of python
- Getting started with vivado ip integrator
- Unix for bioinformatics
- Education.splunk
- Rancher slack
- Getting started with excel
- Outlook tutorial 2010
- Counter code
- Lua getting started
- Lesson 1 hobbies
- Unit 1 local environment
- Unit 1 getting started
- Infuecers gone wild
- Hi3ms
- Perl getting started
- Getting started with ft8
- Unit 3 getting started
- Unit 1 getting started
- Poll everywhere register
- Android development getting started
- Tipos de habilidades del pensamiento
- Getting started with eclipse
- Mathematica getting started