0% found this document useful (0 votes)
71 views

Python For Absolute Beginners

This document provides an introduction to Python programming concepts including what programming is, how to use CodeSkulptor as an online Python interpreter, basic math operations in Python, strings, variables, data types including integers, floats, booleans and lists, conditional logic using if/else statements, loops, and functions. It encourages the reader to experiment with these concepts in CodeSkulptor.

Uploaded by

Venu Gadadasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Python For Absolute Beginners

This document provides an introduction to Python programming concepts including what programming is, how to use CodeSkulptor as an online Python interpreter, basic math operations in Python, strings, variables, data types including integers, floats, booleans and lists, conditional logic using if/else statements, loops, and functions. It encourages the reader to experiment with these concepts in CodeSkulptor.

Uploaded by

Venu Gadadasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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: >>> “It’s a beautiful day!”


>>> “Goodbye, cruel world.”

Try typing one without quotes: >>> Aggies


What’s the result?
>>> “Aggies”
>>> “Rice fight, never die!”
>>> “3 + 2”
Strings

String operators:
concatenation: +
multiplication: *

>>> print “Hello” + “ “ + “world!”


Try concatenating:
>>> print “HAHA” * 250
Try multiplying:
Variables
Variables

Calculate a value: >>> print 21 + 21


42

How can you save


that value?

Give the value a name: >>> ultimate_answer = 42


>>> ultimate_answer
42
Variables
Create a variable >>> headmaster = “Dumbledore”

and give it a value: >>> print headmaster


‘Dumbledore’

>>> headmaster = “Hardcastle”


Now assign a
>>> print headmaster
new value: ‘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: >>> headmaster = “Dumbledore”


>>> print headmaster[2]

Do some math: >>> 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: True or False.

Is 1 equal to 1? >>> print 1 == 1


True

>>> print 15 < 5


Is 15 less than 5?
False
Booleans
What happens when we type
Boolean values in the >>> True
interpreter? >>> False

When the words ‘True’ and >>> true


‘False’ begin with upper case >>> false
letters, Python knows to >>> type(True)
treat them like Booleans >>> type(“True”)
instead of strings or integers.
Booleans
and
If both comparisons are True: >>> 1==1 and 2==2
True
If only one of the
comparisons is True: >>> 1==1 and 2==3
False
If both of the
comparisons are False: >>> 1==2 and 2==3
False
Booleans
or
If both comparisons are True: >>> 1==1 or 2==2
True
If only one of the
comparisons is True: >>> 1==1 or 2!=2
True
If both of the
comparisons are False: >>> 1==2 or 2==3
False
Booleans
not
You can use the word not to >>> 1==1
reverse the answer that True
Python gives:
>>> not 1==1
False

Any expression that is True


can become 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 you’re hungry, let’s eat lunch.”


“If you like Frisbee, let’s play!”

>>> state = “Texas”


If a condition is met, >>> if state == “Texas”:
perform an action: print “TX”

TX
if Statements

Adding a choice: “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.”

Adding a choice in our code


with the else clause: >>> if state == “Texas”
print “TX”
else:
print “[inferior state]”
if Statements

Adding many choices: “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!”
Adding more choices in our code
with the elif clause: 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?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy