Python snippets

From ZENotes
Revision as of 17:35, 1 May 2013 by Admin (Talk | contribs)
Jump to: navigation, search

From http://interactivepython.org, follows snippets (very short bits of code you do as try outs)

Contents

Hello World

   #---------------------------------------------------
   # This demo program shows off how elegant Python is!    
   # Written by Joe Soap, December 2010.
   # Anyone may freely copy or modify this program.
   #---------------------------------------------------
   
   print("Hello, World!")     # Isn't this easy!


(this should work with Syntax Highlighting)

If you are not sure what class a value falls into,

Python has a function called type which can tell you.


   print(type("Hello, World!"))
   print(type(17))
   print("Hello, World")


"Oh no", she exclaimed, "Ben's bike is broken!"

   print('''"Oh no", she exclaimed, "Ben's bike is broken!"''')


This message will span several lines of text

   message = """This message will
   span several
   lines."""
   print(message)
   
   print("""This message will span
   several lines
   of the text.""")   


Variables

message = "What's up, Doc?"
n = 17
pi = 3.14159

print(message)
print(n)
print(pi)

How many ?

minutes = 645
hours = minutes / 60
print(hours)

This / and that //

In the previous example, what we might have wanted to know was how many whole hours there are, and how many minutes remain. Python gives us two different flavors of the division operator. The second, called integer division, uses the token //. It always truncates its result down to the next smallest integer (to the left on the number line).

print(7 / 4)
print(7 // 4)
minutes = 645
hours = minutes // 60
print(hours)

Actual output in hours and minutes

#!/usr/bin/env python

minutes = 645                                                                       # Variable 1: total number of minutes
hours = minutes // 60                                                               # variable 2 uses a // division, takes the smaller integer.
rem=(minutes-(hours*60))                                                            # variable 3 gathers how many minutes where left out
print((minutes),("minutes"),("make"),(hours),("hours"),("and"),(rem),("minutes"))   # result in -h format

Both ways, the Math Way and the Py way

#!/usr/bin/env python
  
# Define some Variables:
m = 1645
h = m // 60
d = h // 24
 
 # The math Way:
rm = (m-(h*60))

 #The Py Way:
rp = m % 60 

print((m),("min"),("="),(d),("d."),(h),(":"),(rm),("with maths."))
print((m),("min"),("="),(d),("d."),(h),(":"),(rp),("with py."))


Nicer Output:

#!/usr/bin/env python
# An hour/minutes counter, since you cant divide 1645 by 60 and have
# a meaningful answer.

# Define some Variables:
m = 1645
h = m // 60
d = h // 24

# 1645 doesn't divide by 60, how do we get the right answer:
# some more variables; there are here 2 ways of doing that: 

# The math Way:
rm = (m-(h*60))

 #The Py Way:
rp = m % 60

print()
print((" How much"),(m),("minutes make in days, hours, minutes?"))
print()
print(("    "),(d),("d"),(h),("h"),(rm),("m"),("with maths."))
print(("    "),(d),("d"),(h),("h"),(rp),("m"),("with py."))
print()

# Easy no ?

Outputs in:

[jph@1215s bin]$ python pstuff.py

 How much 1645 minutes make in days, hours, minutes?

    1 d 27 h 25 m with maths.
    1 d 27 h 25 m with py.


Get Some Input

Same, with input at the prompt AND lots unnecessary variables: one can just define by 24 or 60, no need to first define the 24 factor, much less to pass it as an integer as a new factor...

#!/usr/bin/env python 
#
# An hour/minutes calculator, taking input from the prompt.
# Define the Input Variable:

minutes_from_input = input("Enter how many minutes: ")

# At this stage, the input is just a string, can't do anything with it!
# One MUST convert it to an integer to perform maths on:
minutes = int(minutes_from_input)

# Following not necessary, actually snobbish:
factorhourday = 24
factorhourminute = 60
factordh= int(factorhourday)
factorhm = int(factorhourminute) 

# Variables declarations and some basic maths:
hours = minutes // factorhm
days = hours // factordh

#Let's solve the problem!
 # The math Way:
hours_remain_in_math = (hours-(days*24)) 
minutes_remain_in_math = (minutes-(hours*60))
 #The Py Way:
hours_remain_with_py = hours % 24
minutes_remain_with_py = minutes % 60

# And get the answer out there.
print()
print((" How much"),(minutes),("minutes make in days, hours, minutes?"))
print()
print(("    "),(days),("d"),(hours_remain_in_math),("h"),(minutes_remain_in_math),("m"),("with maths."))
print(("    "),(days),("d"),(hours_remain_with_py),("h"),(minutes_remain_with_py),("m"),("with py."))
print()


Outputs in:

[jph@1215s bin]$ python pstuff.py
Enter how many minutes: 12745

 How much 12745 minutes make in days, hours, minutes?

     8 d 20 h 25 m with maths.
     8 d 20 h 25 m with py.

[jph@1215s bin]$ 

Back to Python

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox