An Intro to Python in 30 minutes


Published on

This is an introduction to basic Python constructs just to initiate executing Python code in minutes

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • - needs Software Freedom Day@Alexandria University
  • Write most useful links for beginners starting
  • Write something more interactive
  • An Intro to Python in 30 minutes

    1. 1. An Introduction to Python SNAKES ON THE WEB:- Python Sumit Kumar Raj
    2. 2. Contents● What is Python ???● Why Python ???● Who uses Python ???● Running Python● Syntax Walkthroughs● Python Twitter API● Coding Mantras
    3. 3. What is Python ??? General purpose, object-oriented, high level programming language Widely used in the industry Used in web programming and in standalone applications
    4. 4. History● Created by Guido von Rossum in 1990 (BDFL)● Named after Monty Pythons Flying Circus●● Blog● Now works for Dropbox
    5. 5. Why Python ???● Readability, maintainability, very clear readable syntax● Fast development and all just works the first time...● very high level dynamic data types● Automatic memory management● Free and open source ● Implemented under an open source license. Freely usable and distributable, even for commercial use.● Simplicity, Availability (cross-platform), Interactivity (interpreted language)● Get a good salaried Job
    6. 6. Batteries Included● The Python standard library is very extensive ● regular expressions, codecs ● date and time, collections, theads and mutexs ● OS and shell level functions (mv, rm, ls) ● Support for SQLite and Berkley databases ● zlib, gzip, bz2, tarfile, csv, xml, md5, sha ● logging, subprocess, email, json ● httplib, imaplib, nntplib, smtplib ● and much, much more ...
    7. 7. Who uses Python ???
    8. 8. Hello World In addition to being a programming language, Python is also an interpreter. The interpreter reads other Python programs and commands, and executes themLets write our first Python Program print “Hello World!”
    9. 9. Python is simpleprint "Hello World!" Python#include <iostream.h> C++int main(){cout << "Hello World!";}public class helloWorld{ public static void main(String [] args) Java { System.out.println("Hello World!"); }}
    10. 10. Lets dive into some code Variables and types>>> a = Hello world! # this is an assignment statement>>> print aHello world!>>> type(a) # expression: outputs the value in interactive mode<type str> • Variables are created when they are assigned • No declaration required • The variable name is case sensitive: ‘val’ is not the same as ‘Val’ • The type of the variable is determined by Python • A variable can be reassigned to whatever, whenever>>> n = 12 >>> n = apa>>> print n >>> print n12 apa>>> type(n) >>> type(n)<type int> <type str>>>> n = 12.0>>> type(n)<type float>
    11. 11. Strings: format()>>>age = 22>>>name = Sumit>>>len(name)>>>print “I am %s and I have owned %d cars” %(“sumit”, 3)I am sumit I have owned 3 cars>>> name = name + ”Raj”>>> 3*name>>>name[:]
    12. 12. Do it ! Write a Python program to assign your USN and Name to variables and print them. Print your name and house number using print formatting string “I am %s, and my house address number is %d” and a tuple
    13. 13. Strings... >>> string.lower() >>> string.upper() >>> string[start:end:stride] >>> S = ‘hello world’ >>> S[0] = ‘h’ >>> S[1] = ‘e’ >>> S[-1] = ‘d’ >>> S[1:3] = ‘el’ >>> S[:-2] = ‘hello wor’ >>> S[2:] = ‘llo world’
    14. 14. Do it... 1) Create a variable that has your first and last name 2) Print out the first letter of your first name 3) Using splicing, extract your last name from the variable and assign it to another 4) Try to set the first letter of your name to lowercase - what happens? Why? 5) Have Python print out the length of your name string, hint use len()
    15. 15. Indentation● Python uses whitespace to determine blocks of code def greet(person): if person == “Tim”: print (“Hello Master”) else: print (“Hello {name}”.format(name=person))
    16. 16. Control Flowif guess == number: while True: #do something #do something #break when doneelif guess < number: break else: #do something else #do something when the loop endselse: #do something elsefor i in range(1, 5): for i in range(1, 5,2): print(i) print(i)else: else: print(The for loop is over) print(The for loop is over)#1,2,3,4 #1,3
    17. 17. Data Structures● List ● Mutable data type, array-like ● [1, 2, 4, “Hello”, False] ● list.sort() ,list.append() ,len(list), list[i]● Tuple ● Immutable data type, faster than lists ● (1, 2, 3, “Hello”, False)● Dictionary ● {42: “The answer”, “key”: “value”}
    18. 18. Functions def sayHello(): print(Hello World!)● Order is important unless using the name def foo(name, age, address) : pass foo(Tim, address=Home, age=36)● Default arguments are supported def greet(name=World)
    19. 19. Functionsdef printMax(x, y): Prints the maximum of two numbers. The two values must be integers. x = int(x) # convert to integers, if possible y = int(y) if x > y: return x else: return yprintMax(3, 5)
    20. 20. Input & Output #input something = input(Enter text: ) #output print(something)
    21. 21. FilesmyString = ”This is a test string”f = open(test.txt, w) # open for writingf.write(myString) # write text to filef.close() # close the filef = open(test.txt) #read modewhile True: line = f.readline() if len(line) == 0: # Zero length indicates EOF break print(line, end=)f.close() # close the file
    22. 22. Linux and Python ”Talk is cheap. Show me the code.” Linus Torvalds
    23. 23. A small code to get tweets ... from twython import Twython twitter = Twython() # First, lets grab a users timeline. Use the screen_name parameter with a Twitter user name. user_timeline = twitter.getUserTimeline(screen_name="sumit12dec",) #count=100, include_rts=1 for tweet in user_timeline: print tweet["text"]
    24. 24. More Resources●●●●● How to Think Like a Computer Scientist, Learning with Python Allen Downey, Jeffrey Elkner, Chris Meyers● Google
    25. 25. Coding Mantras InterviewStreet ProjectEuler GSoC BangPypers Open Source Projects
    26. 26. Any Questions ???
    27. 27. Thank YouReach me 285 524