7 minutes to Learn Python Programming Right Now

Hi , Its a quick tutorial and I have only 7 minutes of your precious time So lets start without any further ado.

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 :

[python] print("Hello World") [/python]

 

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

[python]
## 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

[/python]

[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]
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)
[/python]

[code] Hi I am Function
3[/code]

 

Exceptions

[python]

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.

[/python]

File I/O

[python]f =open("myFile.txt","w")
>>> f.readline()
‘This is the first line of the file.n’
or

for line in f:
print line,
[/python]

More IO operations here

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.

 

One thought on “7 minutes to Learn Python Programming Right Now

Leave a Reply

Your email address will not be published. Required fields are marked *