Hi , Its a quick tutorial and I have only 7 minutes of your precious time So lets start without any further ado.
Contents
Installing Python :
- If you are on Linux or Mac Machine Python is already installed on your system .
- On Windows system Simply to the https://www.python.org/downloads/ and download the version 2.7.x where x can be anything(My case 9) , see the image below for further reference.
- Just add Python to you Path on windows using this Link
Hello World Saga :
1 | print("Hello World") |
Data Type , Strings with Syntax
Basic syntax is same any other programming language like Java , The data structures available in python are lists, tuples and dictionaries. Sets module provides classes for constructing and manipulating unordered collections of unique elements. Common uses include removing duplicates from a sequence.
- Lists are like one-dimensional arrays (but you can also have lists of other lists). Tuples are immutable one-dimensional arrays
- Dictionaries are associative arrays (a.k.a. hash tables)
- Strings – Strings are immutable in python , Below image shows how you can assign a variable with String value and Concatenate it with other data types like Int , Boolean etc.
Loops
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ## This is how you take a range in python myrange = range(10) print(myrange) for item in myrange: if item in (1,3,5,7,9): ## if item % 2 == 0 for other logic print("Old Number") else: print("Even Number") item = 1 while item < 10: print(myrange[item]) item = item+1 |
[code] Output : range(0, 10) Even Number Old Number Even Number Old Number Even Number Old Number Even Number Old Number Even Number Old Number
1 2 3 4 5 6 7 8 9 [/code]
Functions and Claases
[python]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class myClass: def myFunction(self): print("Hi I am Function") def myFunctionWithReturn(self): myText = "How are you" return myText def myFucntionWithArguments(self,number1,number2): return number1+number2 if __name__== "__main__": myObj = myClass() ## Creating Object myObj.myFunction() myValue = myObj.myFucntionWithArguments(1,2) print(myValue) |
Hi I am Function 3
Exceptions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def some_function(): try: # Division by zero raises an exception 10 / 0 except ZeroDivisionError: print "Oops, invalid." else: # Exception didn't occur, we're good. pass finally: # This is executed after the code block is run # and all exceptions have been handled, even # if a new exception is raised while handling. print "We're done with that." |
>>> some_function()
Oops, invalid.
We’re done with that.
File I/O
1 2 | f =open("myFile.txt","w") >>> f.readline() |
1 2 3 4 5 | 'This is the first line of the file.n' or for line in f: print line, |
I hope I was able to give you a quick glimpse of Python in 7 minutes. Please leave comments if you believe there is something that could be improved or added or if there is anything else you would like to see.
Hi admin, Thanks for the Post