Chapter -1: First Programme (Python)
For learn how to download and install python link here: How to download and install python in window
For learn how to run and execute the code in python link here:How to write and run the code
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
Example-1:- Printing the number or letter in python.
Code:
print('3+4')
print(3+4)
Result:
3+4
7
Explanation: In the 1st line write, first we write print('3+4') , here 3+4 in quotes but in second line without quotes and due to this we get two different result.
You see the result of first line is '3+4' but in second line 7, which means if you write anything between quotes it directly print and if you write without quotes then it give the result by doing its calculation.
Example-2:- More example on print command
Code:
print('The value of 3+4 is', 3+4)
print('A', 1, 'XYZ', 2)
Result:
The value of 3+4 is 7
A 1 XYZ 2
Explanation: Here in the first line we write, 'The value of 3+4' in quotes so it directly print but after this we write 3+4 which without quotes so python now calculate this value and print in result.
Thing between quotes directly print and value without quotes first calculate and then give result.
In the 2nd line, we use 'A' in quotes so it directly print in result.
Then we use 1 but without quotes so it also directly print.
Then we use 'XYZ' in quotes so it directly print in result.
Then we use 2 without quotes and it also directly print.
Now question arise in your mind?
Question:- Why 1,2 print in result without quotes but if we use letter without quotes then it show error?
Answer:- Actually python have directory in which it can find number but not letter. So, when it find number then it directly print but if you use any letter like A,B etc. without quotes then it show error because it not find its value.
Example-3:- Use of sep command
Code:
print ('The value of 3+4 is', 3+4,'.')
print ('The value of 3+4 is', 3+4,'.', sep='')
Result:
The value of 3+4 is 7 .
The value of 3+4 is7.
Explanation: In the 2nd line you see sep command which is used to separate two consecutive word from other. In the 1st line ( 'The value of 3+4 is', 7, dot) are away from each other but in 2nd line it is continuous with 7 without any space.
Now question arise in your mind?
Question:- As I told above sep command is use to separate the two consecutive word but when you use this command in 2nd line in result it move dot(.) near to 7. Is this contradict to its function?
Answer:- No, it not contradict its function, because in 2nd line we use sep='' , here between two quotes their is no space use so it now bring consecutive word near to each other without space i.e. 1st word- 'The value of 3+4 is' , 2nd word=3+4 & 3rd word= '.'
Here these three consecutive word now close to each other because their is no space between quotes.
If you put space in between these two quotes then these word away from each other.
Same, if you give two space between quotes then these word are two space away from each other.
Example-4: Sep command
Code:print ('The value of 3+4 is', 3+4, '.')
print ('The value of 3+4 is ', 3+4, '.', sep='@')
Result:
The value of 3+4 is 7 .
The value of 3+4 is @7@.
Explanation : As I explained above if we put anything in between quotes in sep command it print in between the consecutive word. Here In 2nd line their is 3 consecutive word.
1st word= 'The value of 3+4 is' , 2nd word= 3+4, 3rd word= '.'
In this example we put '@' in between quotes so between all these consecutve word @ print as you can see in answer.
Example-5: Print command
Code:print('On the first line')
print('On the second line')
Result:
On the first line
On the second line
Explanation: Here you see we use only command print i.e., used to print the information. In result you get the same information that you print in between the quotes.
Example-6: End command
Code:print('On the first line', end='')
print('On the second line')
Result:
On the first lineOn the second line
If you give space in quotes end='' , then two line print with one space.
But here in this example we give no space in between the quotes so it print two line without space.
Question:- Difference between 'sep' and 'end' command?
Answer:- Sep command is work between two consecutive word but end command work between two line.
Example-7:- More example of End command
Code:print('On the first line', end='@')
print('On the second line')
Result:
On the first line@On the second line
Explanation: Here we add @ in between the "end" that work as put the two line close together by put @ in between them.
Example-8: To get input from the user
Code:
Here, In 1st line first we take value from user by use input command. Example here we write Guru when it ask name.
Then in next line we use ('Hello') which is in quotes so it directly print but after this we use name without quotes now python try to find its value as we write above its value Guru so it print Guru.
Example-9:- Write a programme to in which ask user to enter temperature in Celsius and covert this temperature into Fahrenheit and show the result.
Code:
temp = eval(input('Enter a temperature in Celsius: '))
print('In Fahrenheit, that is', 9/5*temp+32)
Result:
Enter a temperature in Celsius: 40(when we enter 40)
In Fahrenheit, that is 104.0
Explanation: "eval" code is use for get the value in number format and to calculate the number but "input" use for get the input which may be in number or text form.
Here, eval and input are the in built function of python. So, when you use these in built function after write these function , use open bracket and then give the information which you want to give and then close the bracket. Don't change the spelling of code, don't change the format of code, Don't forget to put comma, bracket etc. It may show error or may be change the result of that code.
Question-1:- In this example, we use eval and input both in first line of code. What is the use of both(eval and input) because by the use of "input" we can get the input in both number and text format so why we use eval?
Answer:- Because in this example to convert the temperature in Fahrenheit we use number, for calculation we need "eval" function. That's why we use both.
Question-2:- In previous example we use only input command and it give result. So why eval function is necessary to add in this code?
Answer:- input command is use to take input from user which may be letter or number but eval use for
calculation purpose of number , so use this eval function otherwise it show error because it not able to calculate the number.
"You can use 'int' function instead of 'eval' it also give the same result."
Example-10:- Write a programme and ask user for two input value and then show the average of these two given value.
Code:
num1 = eval(input('Enter the first number: '))
num2 = eval(input('Enter the second number: '))
print('The average of the numbers you entered is', (num1+num2)/2)
Result:
Enter the first number: 10
Enter the second number: 20
The average of the numbers you entered is 15
Explanation: You see we choose num1 and num2. You can choose any name here instead of num1 and num2.
"eval" which is use for calculation and "input" take information from user respectively.
In 1st line and 2nd line you see input which is used to take information but in front of input we use eval command which use calculate the number.
In 3rd line you see 'The average of the numbers you entered is' in quotes and after we use (num1+num2)/2 which means we add first num1 and num2 and then we divide them by 2 .
Example-11:- Square the number
Code:
num=eval(input('Enter a number:'))
print('Your number square', num*num)
Result:
Enter a number: 27
Your number square: 729
Explanation: First of all we take input from user by use input command. But if you want to do some calculation then use eval or int function , so we put eval in front of input.
In next line we give command, i.e., num*num so by the help of eval command it able to calculate this value and show in result.
No comments:
Post a Comment
If you have any doubt, let me know.