"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

December 09, 2012

Python Basics

Two Coursera classes (An Introduction to Interactive Programming in Python, Learn to Program: The Fundamentals) provide very good python beginner learning materials. Python is a Interpreted Language

Quick Summary and Python Notes
Basic Examples and beginner notes posted based on course notes. All Code can be tried in CodeSkulptor.  There are a few syntax differences but this is similar to C programming language.

Example1 - Arithmetic Expressions
  • Basic Arithmetic Expressions
  • Variables, Assignments, Sample Exercise in link
All Scripts can be run in FireFox / Chrome in CodeSkulptor (Not in IE)



Example 2 - Working with Functions
Built In functions - max, abs
Example 3 - Custom Functions
  • Example function to sum up two numbers
Example 4 - If Statement - Conditons , Else If and Else Operator
Example 5 - For Loop - Iteration
Example 6 - While Loop iteration
#https://github.com/swaroopch/byte-of-python/blob/master/programs/ds_using_dict.py
#using dictionary
ab = {
'Swaroop':'swaroop@swaroop.ch',
'Larry':'larry@wall.org',
'Matsumoto':'mat@ruby.org',
'Spammer':'spammer@hotmail.com'
}
#print
print('Swaroop address is', ab['Swaroop'])
#Iterate
for name, address in ab.items():
print('Contact {} at {}'.format(name,address))
#Add Value
ab['Siva'] = 'sivaram2k10@gmail.com'
for name, address in ab.items():
print('Contact {} at {}'.format(name,address))
#https://github.com/swaroopch/byte-of-python/blob/master/programs/while.py
#https://github.com/swaroopch/byte-of-python/blob/master/programs/io_pickle.py
#https://github.com/swaroopch/byte-of-python/blob/master/programs/more_lambda.py
#https://github.com/swaroopch/byte-of-python/blob/master/programs/oop_subclass.py
#https://github.com/swaroopch/byte-of-python/blob/master/programs/exceptions_finally.py
#https://github.com/swaroopch/byte-of-python/blob/master/programs/ds_using_tuple.py
number = 23
running = True
while running:
guess = int(input('Enter an integer:'))
if guess == number:
print('You guessed it')
running = False
elif guess < number:
print('Number low')
else:
print('Number High')
else:
print('While Loop Over')
print('Done')
#https://github.com/swaroopch/byte-of-python/blob/master/programs/ds_using_list.py
shoplist = ['apple','mango','carrot','banana']
print('I have',len(shoplist),'items to purchase')
print('These items are:', end=' ')
for item in shoplist:
print(item,end=' ')
#append an item
shoplist.append('rice')
print('New Shopping List',shoplist)
print('Sorted Shopping List')
shoplist.sort()
print(shoplist)
print('First Item',shoplist[0])
del shoplist[0]
print('First Item',shoplist[0])
print(shoplist)
#https://github.com/swaroopch/byte-of-python/blob/master/programs/stdlib_logging.py
import os
import platform
import logging
if platform.platform().startswith('Windows'):
logging_file = os.path.join(os.getenv('HOMEDRIVE'), os.getenv('HOMEPATH'),'test.log')
else:
logging_file = os.path.join(os.getenv('HOME'),'test.log')
print(logging_file)
logging.basicConfig(
level=logging.DEBUG,
format = '%(asctime)s : %(levelname)s : %(message)s',
filename = logging_file,
filemode='w'
)
logging.debug('Start of program')
logging.info('Do Something')
logging.warning('Dying now')
Happy Learning!!!

No comments: