Posts

Iterate over string

  string= 'Aamir Raza' for i in string: print (i) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/iterate over string.py" A a m i r R a z a Process finished with exit code 0

string concatenation, string deletion

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> fruit1="apple" >>> fruit2="orange" >>> del fruit 1 SyntaxError: invalid syntax >>> del fruit1 >>> fruit1 Traceback (most recent call last):   File "<pyshell#4>", line 1, in <module>     fruit1 NameError: name 'fruit1' is not defined >>> fruit2 'orange' >>> word1="My" >>> word2="name" >>> word3="is" >>> word4="Aamir" >>> word1+word2+word3+word4 'MynameisAamir' >>> word1+" "word2 SyntaxError: invalid syntax >>> word1 'My' >>>  = >>> word1+" " word2 SyntaxError: invalid syntax >>> word1+" "+word2 ...

slicing string like slicing an apple:-using idle

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> fruit="pomegranate" >>> ##slicing >>> ##string name[start,stop,step] >>> fruit="pomegranate" >>> ##by default start is '0', stop is 'n', step is '1'    by rght hand side:-start by default is -1, stop is 'n' and step is 1(remember this). >>> fruit[1:2:3] 'o' >>> fruit[0:11:3] 'peat' >>> fruit[-1:6] '' >>> fruit[-1:11:-1] '' >>> fruit[-1:10:-1] '' >>> fruit[-1::-1] 'etanargemop' >>> fruit[::] 'pomegranate' >>> fruit[-1:-11:-1] 'etanargemo' >>> fruit[-1:-12:-1] 'etanargemop' >>> fruit[:6:] 'pomegr' >>> fruit[...

string indexing:-using idle

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> string="believer" >>> first_string[0] Traceback (most recent call last):   File "<pyshell#1>", line 1, in <module>     first_string[0] NameError: name 'first_string' is not defined >>> first_string="believer" >>> first_string[0] 'b' >>> first string[-1] SyntaxError: invalid syntax >>> first_string[-1] 'r' >>> 

sum of integers from "1" to "n".

  ##sum of integers from '1' to 'n' without loop n= int ( input ( "enter a number:-" )) print ( sum ( list ( range ( 1 , n+ 1 )))) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/sum ofn integers.py" enter a number:-77777777 3024691336419753 Process finished with exit code 0 with loop:- print ( "with loop" ) sum= 0 for i in range ( 1 , n+ 1 ): sum+=i ##sum=sum+i print (sum) with loop 55 Process finished with exit code 0

Calculator

  while True : ##looping forever option= input ( "enter 'add','subtract','divide',multiply,'power','quit'" ) if option== 'quit' : break elif option== "add" : a= int ( input ( "enter a number 1:-" )) b= int ( input ( "enter a number 2:-" )) print (a +b) elif option== "subtract" : a= int ( input ( "enter a number 1:-" )) b= int ( input ( "enter a number 2:-" )) print (a-b) elif option== "divide" : a= int ( input ( "enter a number 1:-" )) b= int ( input ( "enter a number 2:-" )) print (a/b) elif option== "multiply" : a= int ( input ( "enter a number 1:-" )) b= int (inout( "enter a number 2:-" )) print (a*b) elif option== "power" : a= int ( input ( "enter a numb...

for loops , range

  numbers= list ( range ( 1 , 20 , 2 )) print (numbers) ##range(start,end,step) for i in range ( 1 , 10 ): print (i) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/loops,range.py" [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] 1 2 3 4 5 6 7 8 9 Process finished with exit code 0