Last active
August 29, 2015 13:58
-
-
Save alberthdev/9985329 to your computer and use it in GitHub Desktop.
Python Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python Quick Start v1.2.2 (UPDATED: 4/4/14 8:30 PM EDT) | |
# Copyright (c) 2014 Albert Huang ([email protected] / alberthuang.me) | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
######################################################################## | |
# This is a comment. | |
import os | |
import sys | |
print("Hello, world!") | |
#inp = raw_input() | |
inp = raw_input("Enter something: ") | |
print "You entered: ", inp | |
ninp = int(raw_input("Enter a number: ")) | |
print "You entered number %i, plus 1 is %i!" % (ninp, ninp + 1) | |
a = 5 | |
b = 3 | |
if b < a: | |
print "b < a is TRUE" | |
else: | |
print "b < a is FALSE" | |
# if b == a: | |
# if b > a: | |
# if b <= a: | |
# if b >= a: | |
c = 0 | |
while b < a: | |
print "Keep going!" | |
c += 1 | |
if c == 3: | |
break | |
# Equivalent to for (i = 0; i < 5; i++) {} | |
for i in range(0, 5): | |
print i | |
####################################### | |
# Arrays | |
arr = [ "hello", "world", "land" ] | |
for mystr in arr: | |
print "======================================" | |
print mystr | |
print mystr.upper() | |
print mystr[::-1] | |
print mystr.upper()[::-1] | |
print "Trim 2 from front:" | |
print mystr[2:] | |
print "Trim 2 from end:" | |
print mystr[:-2] | |
print "Just first 2:" | |
print mystr[:2] | |
print "Every other letter:" | |
print mystr.upper()[::2] | |
print "--------------------------------------" | |
####################################### | |
# Dictionaries - key, value | |
dct = { | |
"hello": "world", | |
"pig": "oink", | |
"one": 1, | |
} | |
for key in dct: | |
print "KEY %s : VALUE %s" % (key, dct[key]) | |
####################################### | |
# Complicated stuff | |
####################################### | |
####################################### | |
# File I/O | |
fh = open("text.txt", "w") | |
fh.write("Cool beans!\n") | |
fh.close() | |
fh2 = open("text.txt", "r") | |
stuff = fh2.read() | |
fh2.close() | |
print "File contents:" | |
print "------------------" | |
print stuff | |
print "------------------" | |
# Delete stuff | |
os.remove("text.txt") | |
###################################### | |
# OS detection. Yay! | |
print "Hmm, what OS are you using?" | |
if sys.platform == "linux2": | |
print "I found Linux!" | |
elif sys.platform == "win32": | |
print "I found Windows!" | |
elif sys.platform == "darwin": | |
print "I found Mac OS X!" | |
## DISCLAIMER: This may be inaccurate... but only on special systems! | |
## In most cases, you don't need to worry about it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment