Better ask for forgiveness than permission : The Python Way

Initially published on 16/04/2015 9108731_orig Well, if you are starting to learn python one of the best sites to start with is codeacademy.  We can get the python interpreter from it's official site(Here's the link). There are two versions 3.4 and 2.7, we can go for version 2.7 as it is more compatible. Some of the starter inputs(Most of this is from codeacademy tutorials, you can visit the site directly and work online on the site) are as follows: Variables: As with most programming languages, assigning a value for a variable is as simple as: my_variable = 10 Booleans: The two switch states True or False can be assigned to a variable. my_variable = True (or) my_variable = False Reassigning variable values: A variable my_int = 10 can be given a value as my_int = 5 in the next line, and the value can be printed using print my_int Indentation: Indentation is one of the most important aspect of python, either use a Tab space or 4 white spaces but not both. Here's an example of a simple python function. def example() variable = 15 return variable print example() Single-line Comment: Use # at the beginning of a line to give a single line comment. # This is a single line comment in python. Multi-line Comments: If the comment is more than a line use double inverted comma's thrice at the start and end of the comment. """ This is a multi-line comment""" Basic Math Operations: All basic math operations such as addition(+), subtraction(-), multiplication(*) and division(%) can be performed similar to other programming languages. >>print 36 + 64 100 >>print 200 - 100 100 >>print 10 * 10 100 >>print 1000 / 10 100 Exponentiation: In python, the function of exponential can be performed using ** keys. >> oranges = 10 ** 2 >> print oranges 100 Modulo: Modulo operation can be performed using %. Let's work on a simple tip calculator problem: Given meal cost = 44.50, tax = 6.75%. The customer pays a tip of 15% on the total cost of the meal. Calculate the total paid by the customer upto 2 decimal places. This is how the python code will looklike: meal = 44.50 tax = 0.0675 tip = 0.15 meal * tax # 44.50 * 0.0675 meal + meal * tax #44.50 + 3.00375 total = meal + meal * tip """meal * tip = 47.50375 * 0.15 = 7.1256 meal + meal * tip = 47.50375 + 7.1256 = 54.6293 Remember BODMAS rule""" print("%.2f"%total) Strings: Assigning strings values and printing them. python = "Hello world!" print("python") Escaping characters: In python both single quotes and double quotes can be used for commenting. When using single quote, the interpreter doesn't understands the code. In such cases we can use escape characters such as backslash(\). 'Isn\'t it the python?' Accessing by Index: We can access any character of a string by indexing. Indexing starts from 0. >>sixth_letter = "python"[5] >>print sixth_letter n String methods: We can calculate length of the string using len() >>python = "Indian" >>len(python) >>print len(python) 6 We can convert lower case string to upper case string >>python = "indian" >>"indian".upper() >>print python.upper() INDIAN We can convert upper case string to lower case string >>python = "INDIAN" >>"INDIAN".upper() >>print python.upper() indian We can convert a variable to a string >>pi = 3.14 >>print str(pi) 3.14 That's it for python basics.

Leave a comment

Please note, comments must be approved before they are published