Getting Started With Python IDLE

Ad Code

Getting Started With Python IDLE

 



Getting Started With Python IDLE


Python IDLE comes included in Python installations on Windows and Mac. We use Python IDLE as an interactive interpreter or as a file editor. For beginning Python IDLE is a great alternative!

File Editor:
Every programmer needs to edit and save text files. Python programs are files with the .py extension that contain lines of Python code. Python IDLE gives us the ability to create and edit these files with ease. Python IDLE also provides basic syntax highlighting, code completion, and auto-indentation.

How to load Python IDLE

Step 1:  Search or Run IDLE(Python)

Step 2: After then following IDLE Shell screen appears.

Step 3: Now click on New option under File menu to write code.

Comments:
Single-line comment begins with a hash(#) symbol and is useful in mentioning that the
whole line should be considered as a comment until the end of line.

A Multi line comment is useful when we need to comment on many lines. In python, triple
double quote(“ “ “) and single quote(‘ ‘ ‘)are used for multi-line commenting.


Taking Input to the User

Python provides the input() function which is used to take input from the user. Let's understand the following example.

Example -

1.     name = input("Enter a name of student:")  

2.     print("The student name is: ", name)  

Output:

Enter a name of student: Vineet
The student name is:   Vineet

By default, the input() function takes the string input but what if we want to take other data types as an input integer number, we need to typecast the input() function into an integer.

print() function used to display message or value of variable on standard output device.

For example -

Example -

1.     a  = int(input("Enter first number: "))  

2.     b = int(input("Enter second number: "))   

4.     print(a+b)  

Output:

Enter first number: 50
Enter second number: 100
150

We can take any type of values using input() function.

Example -
Write code to enter radius value then print Area of Circle. 
r=float(input("Enter value of radius"));
a=(3.14*r*r);
print("Area of circle =",a);

Explanation: 

Step 1: r = float(input("Enter value of radius"))

  • input("Enter value of radius"): This part of the code is used to enter the value for the radius of a circle. The input() function displays the message inside the parentheses (in this case, "Enter value of radius") to the user and waits for the user to type something.
  • float(): Actually the value returned by input() is always a string, we need to convert it to a number. We use float() to convert the input to a floating-point number, which allows for decimal values (e.g., 3.5, 7.2).
  • r = ...: This statement assigns the value that the user entered (after being converted to a float) to the variable r. This will now store the radius of the circle.

Step 2: a = (3.14 * r * r)

  • This line calculates the area of the circle.
  • Formula for area of a circle: The area of a circle is calculated by the formula πr2\pi r^2πr2, where π\piπ (pi) is approximately 3.14, and rrr is the radius.
  • 3.14 * r * r: Here, 3.14 is used as an approximation for the mathematical constant π\piπ, and r * r calculates r2r^2r2 (radius squared).
  • The result of this multiplication is stored in the variable a.

Step 3: print("Area of circle =", a)

  • print("Area of circle =", a): This command prints the string "Area of circle =" followed by the value of a, which is the area of the circle calculated in the previous step.
  • The result will be displayed to the user, showing the area of the circle based on the radius they input.
Example -
Write code to enter Celsius value then print in Fahrenheit. 

c=int(input("Enter Celsius:"));
f=(c*9/5)+32;
print("Fahrenheit=",f);


Post a Comment

0 Comments

Ad Code