Python Primer
Recently I’ve started to play with Python. I’ve heard a lot about this language before. Recently I’ve just completed my ‘Hello World’ program 🙂
You can get Python from here. I use python’s interactive shell as well as PyDev plugin for Eclipse to write codes
#This is a comment #version 3.0 print("Hello World!"); #semicolon is not required but I use this
- Commenting code
#This is a comment
- Assigning values
x = 5 #5 is assigned to x print (x) #prints 5 print (type(x)) # outputs <class 'int'> x, y = 0, 1 x, y = y, x print(x,y) # prints 1, 0
- Conditions
#traditional conditions x , y = 10, 20 if x < y : print ('X is less than Y') # prints 'X is less than Y' #conditional expressions x, y = 0, 1 z = "less than" if x < y else "not less than" print (z) #prints 'less than'
- Writing a function
#defining a function def myFunc(): print('This is a function in python') #invoking the function myFunc() # prints 'This is a function in python'
- Writing a class
class Language: def __init__(self, name = 'Python'): #constructor self.name = name def showName(self): return self.name scripting = Language() #an instance of the Language class print(scripting.showName()) #as you've guessed :), it prints 'Python' procedural = Language('C') #another instance of the Language class print(procedural.showName()) #now it prints 'C' 🙂
- Getting Input from the User
hawkingsFound = input('Enter what hawkings found lately? :)') #prompts the user 'Enter what hawkings found lately? :)' , user enters 'girls are mysterious' print(hawkingsFound) # it prints 'girls are mysterious' 🙂
Leave a comment