Python Diaries chapter 1

Author: Divyanshu aka StellarCoderPython 2.xThis series is the result of my love forPYTHONand now I want to share this love with all of you.
I will introduce you to mystical and charismatic python.Yeah!mystical and charismatic..Don’t you believe me??Let me show you then…To print in pythonprint ‘Hello, World!’Output –> Hello, World!Comments”’This is a comment.”’# This is also a commenthashis used for single line comment andtriple quotesfor multiple linesTo input an integer in pythonn=input()To input Stringa=raw_input()To create an empty array(listin python)arr=[]To assign a value to all elements of an arrayLet us say you wanna make an array of size 10 with each element equal to 0 ie. same value to allarr=[0]*10output –> [0,0,0,0,0,0,0,0,0,0]To add item in array(list)arr.append(1)
print arroutput –> [1,2]while loop syntaxi=0
while i<l:
print i
i=i+1for-in loop syntaxarr=[1,2,3,4]

for item in arr:
print item#output will be each array item printed in separate lineyou can use any thing instead of item like a,b,c it,key ie.literally anything except keywords and reserved wordsTo printprintstatements in same line, just add a comma after your textprint ‘Next print statement after this will be printed in same line’,
print “I’m in first line”Output–>Next print statement after this will be printed in same line I’m in first line’Variant of for in loop.If you want to loop from i=start to end let us say 1 to 10for i in range(1,11):
print iinrange(start,end)–>*start* is inclusive andendis exclusive**, read like this[start,end)If you don’t pass an argument,defaultvalue–>start = 0,end=len(string)if you want to jump by n then range(start,end,jump=n), default value –>jump=0To sort an array(both integer and string)arr=[7,6,4,9,3,2,8,1,5]
arr2=sorted(arr,reverse=True)# output arr1 –> [1,2,3,4,5,6,7,8,9]
#output arr2 –> [9,8,7,6,5,4,3,2,1]NOTE –> In Python, when using true-false, remember to CAPITALIZE FIRST LETTER ie. *Trueand *False**logical operators just type name in english ie. && –> and , || –> or , ! –> notbitwise operators are same as C ie. & , | , ^(XOR)all arithmetic operators are also same as in Cif-else in pythona=5

if a==5:
print “Sweet Mother of Mercy!”

elif a==6:
print “It’s a SIX”

elif a==4:
print “It’s a FOUR”

print “Not an interesting number”Output –> Sweet Mother of Mercy!========Various types of Inputs========If you have to input a variablen=input()If you have to input a character or string (including spaces)a=raw_input()If you want to store words in an array, for eg. if the line is –> save this line in an arrayarr=map(str,raw_input().split())output –> [‘save’,’this’,’line’,’in’,’an’,’array’]If you want to store integers in an array for eg. –> 5 7 3 2 62 7 8arr=map(int,raw_input().split())#output –> [5,7,3,2,62,7,8]If you want to input a line/word and store each character seperately eg.–> divyanshua=list(raw_input())#output –> [‘d’,’i’,’v’,’y’,’a’,’n’,’s’,’h’,’u’]To acces an particular index in arrayprint a[0]
print a[1]ouput–> ‘d’
output –> ‘i’similarly for other indicesSuppose you input an integer as a string, now you want to convert it to integer use thisa=’5’
a=int(a)To convert an integer to stringb=5
b=str(5)To join all items of an arrayarr=[‘d’,’i’,’v’,’y’,’a’,’n’,’s’,’h’,’u’]
c=”.join(arr)output will be–> ‘divyanshu’To reverse an array or stringa=’example’
arr=arr[::-1]output –> ‘elpmaxe’
similarly for array (feeling too tired to type 😉 )That’s it for now……Next article will be posted soon….stay tuned !

News Reporter

Leave a Reply

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