welcome to the python
hello there are some exercises that might be helpful for you to learn python in easy way.
- print('mohsin') or print("mohsin") to print mohsin on screen .
you can also use print to call any previous defined or initialized value like
price = 10
print(price)
this is the code that is going to display value
of price on the screen.
it means when you use print without quoations
like print(price) it will
display the value of the variable that you
entered in the small brackets.
so it means that print can either
display value and it can call the
variables also .
so
it can involve 2 functions in its brackets
examle
if we write this code
print(' * ' *10)
then we will see ********** on screen
which means that the print can executes
multiply the
add subtract and other mathematical in its brackets.
- structure of pythen (how compiler works )
pythen compiler runs line by line from the top .
example
if you initialize the value of price 10
and then again initialize
the same variable then it will
act like updated .
your code will be like.
price=10
price=20
print (price)
then output will be displayed
20 not 10 . because python runs
the program line by line from the top.
- Taking input
we use the label input to take an input from the user
example
if you want user to tell his name then yo can write
input('what is your name ')
what is your name will be make a space on screen for the user
to enter his/her name in the empty space.
if
you want to give an auto reply
service you can use these two tools to make the code
example
name = input('what is your name ')
print ('hi' + name)
if
you want to ask some questions from user
and the requirement is we have to sum his answer
for example
ask a question what is your name then
ask another
question what is your age then
ask another question what is your favourite color
your code will be like
name = input('what is your name ' )age= input('what is your age ')color =input('what is your favourite color')print(name +'likes ' + color + age)you can also set the specific data type like
Data typeint() for non point numbersfloat() for point numbersstr() for the charactersfor exampleyou are asking your user to enter
his date of birth and you wantonly numbers and you want to calculate
his current agethen you can write the code like thisbirth_year =input("birth year") age= 2020 - int(birth_year) print(age)if you have a situation where you have to convert weight from lbs to kg thenyour code will be likeweight =input("enter your weight :lbs") waight = int(weight) * 0.45print(waight)if you want to print first word or any of word that user input then your code would be likename=input("what is your name ")print(name[0])if you want to print second word or any of word that user input then your code would be likename=input("what is your name ")print(name[1])if you want to print secondlast word that user input then your code would be likename=input("what is your name ")print(name[-2])and so onif you want to print first 3 word that user input then your code would be likename=input("what is your name ")print(name[0:3])if you want to print last 3 word that user input then your code would be likename=input("what is your name ") print(name[2:5])if you wrote : then it will devide the number if you write 1:5 then it will skip the value of indexwhich is written in index 0if you want to print with skipping the first and the last word that user input then your code would be likename=input("what is your name ") print(name[1:-1])
Comments
Post a Comment