Summery: Python Tutorial for Beginners. Hope You Enjoy…
In the previous article we’ve talked about conditionals, loops, break and continue statements. In this article we are going to talk about some basic ideas which are little bit complex. So let’s get started.
In simple terms functions are collection of codes that we can use again and again within a program. These block of codes are only get executed when they are called. In python there are many in-built functions such as print(), range(), chr(), compile() etc… You can also create your own functions with your code.
def name(): print(“tutorialsoverflow”)
Above code is a simple function in python. If you execute a python script with the above code block you will never get any output in the terminal because even though there is a function in the script, it is not called. As I mentioned in the above functions get executed only if they get called.
We can call the above function like below.
def name(): print(“tutorialsoverflow”) name()
Now if we run the script once again you will get an output in terminal saying “tutorialsoverflow”.
Below is another function with more stuff inside.
def money(btc): amount = btc + 273 print(amount) money(2)
Read and Write Files
fw = open(‘Sample.txt’, ‘w’) fw.write(‘Writing to a file’) fw.write(‘Hello! there’) fw.close()
Above code block shows you how to write to a file using python. In the first line of the code we’ve created a text file with the name “sample”. In the first line of the code ‘w’ is a parameter which we need to add to the open() function. ‘w’ parameter will overwrite the existing content. If we add ‘a’ instead of ‘w’ it will append to the end of the file. write() function will write the content to the created file and finally close() function will close the opened file after finish writing to it.
fr = open(‘sample.txt’, ‘r’) text = fr.read() print(text) fr.close()
Above code block shows you how to read from a file in python. In the first line of the code we’ve opened the file that we need to read. ‘r’ parameter will specify the open function that we need to read from the open file. In second line we’ve stored the text inside the sample.txt file in the text variable. Finally, we print the stored text on to the terminal.
Classes and Objects in Python
Class is an easy way to group similar variables, functions together. In common practice classes are started with capital letters.
class Enemy: life = 3 def attack(self): print("Ouch") self.life -= 1 def checklife(self): if self.life <= 0: print("I'm Dead") else: print(str(self.life) + "life left") enemy1 = Enemy() enemy1.attack() enemy1.attack()
So when using classes in python there are few things that you need to remember. When you call variables inside a class you always need to add “self.” In front of the variable. Always we need to create an object in order to access data in classes. In the above example enemy1 is the object that we used to call the enemy class. Last two lines of the code shows you how we use functions inside a class.
Each object is independent from each other. Which means; if we make a change to one object which uses the same class it does not affect for the second object which also use the same class.
Also Read: Python Encryption and Decryption