Python is an object-oriented programming language which is gaining popularity in building complex applications. It is used in different fields, eg create web applications, handle big data, perform complex mathematics and also considered by data scientist to analyze their problems. This page is a simple writeup of basic python notes, the code statements which are normally used in almost all the scenarios.
Basic Python Notes
1. Print Variable
Normally done by a simple print statement. Note that there is no ";" or any phrase used to end the statement.
print("This is the output string") print($myVar)
2. Strings Manipulations
A string in python is an array of bytes with 0 indexing the first character.
s = "FIRST python documentation" s = F s = I #Extract the second word from the string. second_word = s[6:12] #where the 6th index is pointing at "p" and 12 is pointing at the whitespace after "n". #String returned is "python".
3. If Statement
The condition is written using the simple logical operators "==", ">=", etc. The if/else statement is terminated by ":"(colon).
a = 10 b = 11 if a == b: print("The numbers are same") else: print("The numbers are different") #Output = "The numbers are different". #Simple if - elif - else condition example if a > b: print("a ia greater than b") elif b > a: print("b is greater than a") else: print("Numbers are equal")
4. For Loop
Similar to if statement, for is also terminated with colon symbol -
#Print each array value and append " Learning" whenever the iterated value is "Python" languages = ["PHP", "C++", "Python", "JAVA"] for x in languages: if x == "Python": print(x + " Learning") else: print(x) #The above snippet will print "PHP" on the first iteration and #then "C++", "Python Learning" and "JAVA" #"+" symbol is used to concatenate two string values.
Set of code statements which can be called to perform/return a specific task/values.
def test_function(food): for x in languages: if x == "Python": print(x + " Learning") else: print(x) languages = ["PHP", "C++", "Python", "JAVA"] test_function(languages) #Now test functions can be called multiple time from #different places to return which language is in the process of learning. #Returns the same output as above for loop example.