SlideShare a Scribd company logo
1 of 52
Python 101: 
Python for Absolute Beginners 
PyTexas 2014
What is programming?
A computer is a machine that stores 
pieces of information. 
A computer also moves, arranges, and 
controls that information (or data). 
A program is a detailed set of 
instructions that tells a computer 
what to do with that data.
CodeSkulptor!
CodeSkulptor 
 Developed by Scott Rixner of Rice 
University to use for COMP 200. 
 Based on CodeMirror and Skulpt. 
 www.codeskulptor.org 
 If you want to learn more about 
using Python with CodeSkulptor 
after this class, check out the 
Coursera course “An Introduction 
to Interactive Programming in 
Python”! (9/15 – 11/16) 
 https://www.coursera.org/course/ 
interactivepython
Interacting with CodeSkulptor 
 Run 
 Save 
 Fresh URL 
 Open Local 
 Reset
Additional Resources 
 Docs (documentation) 
 Demos 
 Viz Mode
Let’s see what this thing can do…
Recap: 
Explore! Makes changes, see how they 
impact the program as a whole. 
When in doubt, check the 
documentation. 
When super in doubt, Google it.
Math
Math 
Try typing this into CodeSkuptor: 
>>> print 3 + 12 
>>> print 12 - 3 
>>> print 9 + 5 – 15 + 12 
Operators: 
add: + 
subtract: - 
Note: don’t type the arrows >>> !
Math 
Rule: If you want Python to answer in floats, you have to talk to it in floats. 
More operators: 
divide: / 
multiply: * 
>>> print 3 * 12 
>>> print 12 / 3 
>>> print 11 // 3 
>>> print 12.0 / 3.0 
>>> print 11.0 / 3.0 
>>> print 11.0 // 3.0
Math 
Comparison operators: 
== Equal to 
!= Not equal to 
< Less than 
> Greater than 
<= Less than or equal to 
>= Greater than or equal to
Math 
Practice: 
>>> print 2 < 3 True 
>>> print 2 <= 2 False 
>>> print 3 > 2 True 
>>> print 2 != 3 True 
>>> print False < True True
Strings
Strings 
Examples: 
Try typing one without quotes: 
What’s the result? 
>>> “It’s a beautiful day!” 
>>> “Goodbye, cruel world.” 
>>> Aggies 
>>> “Aggies” 
>>> “Rice fight, never die!” 
>>> “3 + 2”
Strings 
String operators: 
concatenation: + 
multiplication: * 
Try concatenating: 
Try multiplying: 
>>> print “Hello” + “ “ + “world!” 
>>> print “HAHA” * 250
Variables
Variables 
Calculate a value: 
How can you save 
that value? 
Give the value a name: 
>>> print 21 + 21 
42 
>>> ultimate_answer = 42 
>>> ultimate_answer 
42
Variables 
Create a variable 
and give it a value: 
Now assign a 
new value: 
>>> headmaster = “Dumbledore” 
>>> print headmaster 
‘Dumbledore’ 
>>> headmaster = “Hardcastle” 
>>> print headmaster 
‘Hardcastle’ 
>>> color = “Brad Neely” 
>>> color = 12
Variables 
 You can calculate a variable once, but keep the result to use later. 
 You can keep the same name for a variable, but change the value. 
Some other things that we can do with variables: 
Get an index from a string: 
Do some math: 
>>> headmaster = “Dumbledore” 
>>> print headmaster[2] 
>>> number = 3 
>>> print headmaster[number - 2]
Types of data
Data Types 
We already know about three types of data: 
“Whoop!” string 
42 integer 
3.14159 float 
Python can tell us about types using the type() function: 
>>> print type(“Whoop!”) 
<type ‘str’> 
How would we get Python to output int and float types?
Data Type: Lists
Lists 
List: a sequence of objects 
>>> Beatles = [“John”, “Paul”, “George”, “Ringo”] 
>>> grades = [82, 93, 67, 99, 100] 
Guess what this will output: 
>>> type(Beatles) 
>>> type(grades)
Lists 
List: a sequence of objects 
>>> Beatles = [“John”, “Paul”, “George”, “Ringo”] 
>>> grades = [82, 93, 67, 99, 100] 
Guess what this will output: 
>>> type(Beatles) 
<type ‘list’> 
>>> type(grades) 
<type ‘list’>
Lists 
Index: Where an item is in the list 
>>> Beatles = [“John”, “Paul”, “George”, “Ringo”] 
>>> Beatles[0] 
‘John‘ 
[“John”, “Paul”, “George”, “Ringo”] 
0 1 2 3 
Python always starts at zero!
Data Type: Booleans
Booleans 
A boolean value can be: 
Is 1 equal to 1? 
Is 15 less than 5? 
True or False. 
>>> print 1 == 1 
True 
>>> print 15 < 5 
False
Booleans 
What happens when we type 
Boolean values in the 
interpreter? 
When the words ‘True’ and 
‘False’ begin with upper case 
letters, Python knows to 
treat them like Booleans 
instead of strings or integers. 
>>> True 
>>> False 
>>> true 
>>> false 
>>> type(True) 
>>> type(“True”)
Booleans 
and 
If both comparisons are True: 
If only one of the 
comparisons is True: 
If both of the 
comparisons are False: 
>>> 1==1 and 2==2 
True 
>>> 1==1 and 2==3 
False 
>>> 1==2 and 2==3 
False
Booleans 
or 
If both comparisons are True: 
If only one of the 
comparisons is True: 
If both of the 
comparisons are False: 
>>> 1==1 or 2==2 
True 
>>> 1==1 or 2!=2 
True 
>>> 1==2 or 2==3 
False
Booleans 
not 
You can use the word not to 
reverse the answer that 
Python gives: 
Any expression that is True 
can become False: 
>>> 1==1 
True 
>>> not 1==1 
False 
>>> not True 
False
Booleans 
You can also use Booleans in their own expressions: 
>>> True and True 
>>> True and False 
>>> False and False 
>>> True or True 
>>> False or True 
>>> False or False 
>>> not True and True 
>>> not True or True
Logic
if Statements
if Statements 
Making decisions: 
If a condition is met, 
perform an action: 
“If you’re hungry, let’s eat lunch.” 
“If you like Frisbee, let’s play!” 
>>> state = “Texas” 
>>> if state == “Texas”: 
print “TX” 
TX
if Statements 
Adding a choice: 
Adding a choice in our code 
with the else clause: 
“If you’re hungry, let’s eat lunch. Or else we 
can eat in an hour.” 
“If you like Frisbee, let’s play! Or else we can 
play rugby.” 
>>> if state == “Texas” 
print “TX” 
else: 
print “[inferior state]”
if Statements 
Adding many choices: 
Adding more choices in our code 
with the elif clause: 
“If you like Frisbee, let’s play! Or else we 
can play rugby. Or else we can play 
Bioshock, or Half-Life, or Portal…” 
>>> if name == “Paige” 
print “Hi Paige!” 
elif name == “Walker”: 
print “Hi Walker!” 
else: 
print “Imposter!”
Loops
Loops 
Loops are chunks of code that repeat a task over and over again. 
 Counting loops repeat a certain number of times. 
 Conditional loops keep going until a certain thing happens 
(or as long as some condition is True).
Loops 
Counting loops repeat a certain number of times – they keep going until they get 
to the end of a count. 
>>> for mynum in [1, 2, 3, 4, 5]: 
print "Hello", mynum 
Hello 1 
Hello 2 
Hello 3 
Hello 4 
Hello 5 
The for keyword is used to create this kind of loop, so it is usually just called a 
for loop.
Loops 
Conditional loops repeat until something happens 
(or as long as some condition is True). 
>>> count = 0 
>>> while (count < 4): 
print 'The count is:', count 
count = count + 1 
The count is: 0 
The count is: 1 
The count is: 2 
The count is: 3 
The while keyword is used to create this kind of loop, so it is usually just called a 
while loop.
Algorithms
Algorithms 
Really just means “set of instructions” 
Secret: computers 
aren’t very smart.
How would I make a pot of coffee? 
1. Get a flavor of ground coffee. 
2. Get a coffee maker. 
3. Get filter paper. 
4. Get a pot of water. 
5. Make sure the coffee maker is plugged in… 
…and on, and on, and on. 
But to us, it’s just “make a pot of coffee”.
Functions
Remember how Algorithms are just instructions? 
 Functions are just a concise way to group instructions into a bundle. 
What it's like in our minds: 
“Make a pot of coffee.”  bundle 
In Python, you could say it like this: 
make_coffee(coffee_grounds, coffee_pot, water, filter_paper) 
^ ^-----------------^---------------^-----------------^ 
function name function parameters
Functions 
Let’s define a function in CodeSkulptor: 
>>> def beverage(): 
print ‘Have you had a cup of coffee today?’ 
Now we’ll call the function: 
>>> beverage() 
Have you had a cup of coffee today?
Functions 
But what if not everyone wants a cup of coffee? 
Let’s define a function with parameters: 
>>> def beverage(drink): 
print “Have you had a cup of ” + drink + “ today?’ 
Now we’ll call the function: 
>>> beverage(“Monster Zero”) 
Have you had a cup of Monster Zero today?
Functions 
 Functions are defined using def. 
 Functions are called using parentheses (). 
 Functions take parameters and return outputs. 
 print displays information, but does not give a value. 
 return gives a value to the caller.
Thanks so much! 
Any questions?

More Related Content

What's hot

Python in 30 minutes!
Python in 30 minutes!Python in 30 minutes!
Python in 30 minutes!Fariz Darari
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python pptPriyanka Pradhan
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonYi-Fan Chu
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming pptismailmrribi
 
python presntation 2.pptx
python presntation 2.pptxpython presntation 2.pptx
python presntation 2.pptxArpittripathi45
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaEdureka!
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for KidsAimee Maree Forsstrom
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonNowell Strite
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in pythonRaginiJain21
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
Introduction to Basics of Python
Introduction to Basics of PythonIntroduction to Basics of Python
Introduction to Basics of PythonElewayte
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | EdurekaEdureka!
 

What's hot (20)

Python in 30 minutes!
Python in 30 minutes!Python in 30 minutes!
Python in 30 minutes!
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
 
python presntation 2.pptx
python presntation 2.pptxpython presntation 2.pptx
python presntation 2.pptx
 
Beginning Python Programming
Beginning Python ProgrammingBeginning Python Programming
Beginning Python Programming
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Python functions
Python functionsPython functions
Python functions
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Introduction to Basics of Python
Introduction to Basics of PythonIntroduction to Basics of Python
Introduction to Basics of Python
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | Edureka
 
Python basic
Python basicPython basic
Python basic
 
Introduction python
Introduction pythonIntroduction python
Introduction python
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Python ppt
Python pptPython ppt
Python ppt
 

Similar to Python 101: Python for Absolute Beginners (PyTexas 2014)

Magic 8 ball prorgramming or structure is fun
Magic 8 ball prorgramming or structure is funMagic 8 ball prorgramming or structure is fun
Magic 8 ball prorgramming or structure is fungeekinlibrariansclothing
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
Computer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptComputer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptRedenOriola
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/elseMarc Gouw
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3aRuth Marvin
 
Learn python
Learn pythonLearn python
Learn pythonmocninja
 
Python language data types
Python language data typesPython language data types
Python language data typesHarry Potter
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data typesLuis Goldster
 
Python language data types
Python language data typesPython language data types
Python language data typesTony Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data typesFraboni Ec
 
Python language data types
Python language data typesPython language data types
Python language data typesJames Wong
 
Python language data types
Python language data typesPython language data types
Python language data typesYoung Alista
 
CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1johnnygoodman
 
Programming with python
Programming with pythonProgramming with python
Programming with pythonsarogarage
 

Similar to Python 101: Python for Absolute Beginners (PyTexas 2014) (20)

Python! An Introduction
Python! An IntroductionPython! An Introduction
Python! An Introduction
 
Magic 8 ball prorgramming or structure is fun
Magic 8 ball prorgramming or structure is funMagic 8 ball prorgramming or structure is fun
Magic 8 ball prorgramming or structure is fun
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
 
Course set three full notes
Course set three full notesCourse set three full notes
Course set three full notes
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python
PythonPython
Python
 
Computer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptComputer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .ppt
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/else
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3a
 
Learn python
Learn pythonLearn python
Learn python
 
Magic 8 ball putting it all together
Magic 8 ball  putting it all togetherMagic 8 ball  putting it all together
Magic 8 ball putting it all together
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1
 
Programming with python
Programming with pythonProgramming with python
Programming with python
 

Recently uploaded

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 

Recently uploaded (20)

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 

Python 101: Python for Absolute Beginners (PyTexas 2014)

  • 1. Python 101: Python for Absolute Beginners PyTexas 2014
  • 3. A computer is a machine that stores pieces of information. A computer also moves, arranges, and controls that information (or data). A program is a detailed set of instructions that tells a computer what to do with that data.
  • 5. CodeSkulptor  Developed by Scott Rixner of Rice University to use for COMP 200.  Based on CodeMirror and Skulpt.  www.codeskulptor.org  If you want to learn more about using Python with CodeSkulptor after this class, check out the Coursera course “An Introduction to Interactive Programming in Python”! (9/15 – 11/16)  https://www.coursera.org/course/ interactivepython
  • 6. Interacting with CodeSkulptor  Run  Save  Fresh URL  Open Local  Reset
  • 7. Additional Resources  Docs (documentation)  Demos  Viz Mode
  • 8. Let’s see what this thing can do…
  • 9. Recap: Explore! Makes changes, see how they impact the program as a whole. When in doubt, check the documentation. When super in doubt, Google it.
  • 10. Math
  • 11. Math Try typing this into CodeSkuptor: >>> print 3 + 12 >>> print 12 - 3 >>> print 9 + 5 – 15 + 12 Operators: add: + subtract: - Note: don’t type the arrows >>> !
  • 12. Math Rule: If you want Python to answer in floats, you have to talk to it in floats. More operators: divide: / multiply: * >>> print 3 * 12 >>> print 12 / 3 >>> print 11 // 3 >>> print 12.0 / 3.0 >>> print 11.0 / 3.0 >>> print 11.0 // 3.0
  • 13. Math Comparison operators: == Equal to != Not equal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to
  • 14. Math Practice: >>> print 2 < 3 True >>> print 2 <= 2 False >>> print 3 > 2 True >>> print 2 != 3 True >>> print False < True True
  • 16. Strings Examples: Try typing one without quotes: What’s the result? >>> “It’s a beautiful day!” >>> “Goodbye, cruel world.” >>> Aggies >>> “Aggies” >>> “Rice fight, never die!” >>> “3 + 2”
  • 17. Strings String operators: concatenation: + multiplication: * Try concatenating: Try multiplying: >>> print “Hello” + “ “ + “world!” >>> print “HAHA” * 250
  • 19. Variables Calculate a value: How can you save that value? Give the value a name: >>> print 21 + 21 42 >>> ultimate_answer = 42 >>> ultimate_answer 42
  • 20. Variables Create a variable and give it a value: Now assign a new value: >>> headmaster = “Dumbledore” >>> print headmaster ‘Dumbledore’ >>> headmaster = “Hardcastle” >>> print headmaster ‘Hardcastle’ >>> color = “Brad Neely” >>> color = 12
  • 21. Variables  You can calculate a variable once, but keep the result to use later.  You can keep the same name for a variable, but change the value. Some other things that we can do with variables: Get an index from a string: Do some math: >>> headmaster = “Dumbledore” >>> print headmaster[2] >>> number = 3 >>> print headmaster[number - 2]
  • 23. Data Types We already know about three types of data: “Whoop!” string 42 integer 3.14159 float Python can tell us about types using the type() function: >>> print type(“Whoop!”) <type ‘str’> How would we get Python to output int and float types?
  • 25. Lists List: a sequence of objects >>> Beatles = [“John”, “Paul”, “George”, “Ringo”] >>> grades = [82, 93, 67, 99, 100] Guess what this will output: >>> type(Beatles) >>> type(grades)
  • 26. Lists List: a sequence of objects >>> Beatles = [“John”, “Paul”, “George”, “Ringo”] >>> grades = [82, 93, 67, 99, 100] Guess what this will output: >>> type(Beatles) <type ‘list’> >>> type(grades) <type ‘list’>
  • 27. Lists Index: Where an item is in the list >>> Beatles = [“John”, “Paul”, “George”, “Ringo”] >>> Beatles[0] ‘John‘ [“John”, “Paul”, “George”, “Ringo”] 0 1 2 3 Python always starts at zero!
  • 29. Booleans A boolean value can be: Is 1 equal to 1? Is 15 less than 5? True or False. >>> print 1 == 1 True >>> print 15 < 5 False
  • 30. Booleans What happens when we type Boolean values in the interpreter? When the words ‘True’ and ‘False’ begin with upper case letters, Python knows to treat them like Booleans instead of strings or integers. >>> True >>> False >>> true >>> false >>> type(True) >>> type(“True”)
  • 31. Booleans and If both comparisons are True: If only one of the comparisons is True: If both of the comparisons are False: >>> 1==1 and 2==2 True >>> 1==1 and 2==3 False >>> 1==2 and 2==3 False
  • 32. Booleans or If both comparisons are True: If only one of the comparisons is True: If both of the comparisons are False: >>> 1==1 or 2==2 True >>> 1==1 or 2!=2 True >>> 1==2 or 2==3 False
  • 33. Booleans not You can use the word not to reverse the answer that Python gives: Any expression that is True can become False: >>> 1==1 True >>> not 1==1 False >>> not True False
  • 34. Booleans You can also use Booleans in their own expressions: >>> True and True >>> True and False >>> False and False >>> True or True >>> False or True >>> False or False >>> not True and True >>> not True or True
  • 35. Logic
  • 37. if Statements Making decisions: If a condition is met, perform an action: “If you’re hungry, let’s eat lunch.” “If you like Frisbee, let’s play!” >>> state = “Texas” >>> if state == “Texas”: print “TX” TX
  • 38. if Statements Adding a choice: Adding a choice in our code with the else clause: “If you’re hungry, let’s eat lunch. Or else we can eat in an hour.” “If you like Frisbee, let’s play! Or else we can play rugby.” >>> if state == “Texas” print “TX” else: print “[inferior state]”
  • 39. if Statements Adding many choices: Adding more choices in our code with the elif clause: “If you like Frisbee, let’s play! Or else we can play rugby. Or else we can play Bioshock, or Half-Life, or Portal…” >>> if name == “Paige” print “Hi Paige!” elif name == “Walker”: print “Hi Walker!” else: print “Imposter!”
  • 40. Loops
  • 41. Loops Loops are chunks of code that repeat a task over and over again.  Counting loops repeat a certain number of times.  Conditional loops keep going until a certain thing happens (or as long as some condition is True).
  • 42. Loops Counting loops repeat a certain number of times – they keep going until they get to the end of a count. >>> for mynum in [1, 2, 3, 4, 5]: print "Hello", mynum Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 The for keyword is used to create this kind of loop, so it is usually just called a for loop.
  • 43. Loops Conditional loops repeat until something happens (or as long as some condition is True). >>> count = 0 >>> while (count < 4): print 'The count is:', count count = count + 1 The count is: 0 The count is: 1 The count is: 2 The count is: 3 The while keyword is used to create this kind of loop, so it is usually just called a while loop.
  • 45. Algorithms Really just means “set of instructions” Secret: computers aren’t very smart.
  • 46. How would I make a pot of coffee? 1. Get a flavor of ground coffee. 2. Get a coffee maker. 3. Get filter paper. 4. Get a pot of water. 5. Make sure the coffee maker is plugged in… …and on, and on, and on. But to us, it’s just “make a pot of coffee”.
  • 48. Remember how Algorithms are just instructions?  Functions are just a concise way to group instructions into a bundle. What it's like in our minds: “Make a pot of coffee.”  bundle In Python, you could say it like this: make_coffee(coffee_grounds, coffee_pot, water, filter_paper) ^ ^-----------------^---------------^-----------------^ function name function parameters
  • 49. Functions Let’s define a function in CodeSkulptor: >>> def beverage(): print ‘Have you had a cup of coffee today?’ Now we’ll call the function: >>> beverage() Have you had a cup of coffee today?
  • 50. Functions But what if not everyone wants a cup of coffee? Let’s define a function with parameters: >>> def beverage(drink): print “Have you had a cup of ” + drink + “ today?’ Now we’ll call the function: >>> beverage(“Monster Zero”) Have you had a cup of Monster Zero today?
  • 51. Functions  Functions are defined using def.  Functions are called using parentheses ().  Functions take parameters and return outputs.  print displays information, but does not give a value.  return gives a value to the caller.
  • 52. Thanks so much! Any questions?