9.Simple Programs to Begin with, Comments and statements, IDEs

Program Format and Structure
It is always better to structure your Python program as follows
  • Start with an introductory comment stating the author's name, the purpose of the program and other relevant information.This information should be in the form of comment or document string
Then include statements that do the following
  • Import any modules needed by the program
  • Initialize important variables, suitably commented
  • Prompt the user for input data and save the input data in variables
  • Process the input to produce the results
  • Display the results

Comments
Comments are very important while writing a program. They describe what is going on inside a  program, so that a person looking at the source code does not have a hard time figuring it out.

You might forget the key details of the program you just wrote in a month's time. So taking the time to explain these concepts in the form of comments is always fruitful.

Advantages of Using Comments
Using comments in programs makes our code more understandable. It makes the program more readable which helps us remember why certain blocks of code were written.

Other than that, comments can also be used to ignore some code while testing other blocks of code. This offers a simple way to prevent the execution of some lines or write a quick pseudo-code for the program.

Single-Line Comments in Python
In Python, we use the hash (#) symbol to start writing a comment.It extends up to the newline character. Comments are for programmers to better understand a program. Python Interpreter ignores comments.

#This is a comment 
 print('Hello')

Multi-Line Comments in Python
We can have comments that extend up to multiple lines. One way is to use the hash(#) symbol at the beginning of each line. For example:
#This is a long comment 
#and it extends 
#to multiple lines

Another way of doing this is to use triple quotes, either ''' or """.
While triple quotes are typically used for multiline strings (docstrings), they can also be used for multiline comments. Note that these are technically string literals that are not assigned to any variable, and thus, they are ignored by the interpreter.

"""This is also a 
perfect example of 
multi-line comments"""

How to Write Better Comments?
Use comments to describe what a function does and not the specific details on how the function does it.
Try to remove as many redundant comments as possible. Try writing code that can explain itself, using better function/variable name choice.
Try to make the comments as short and concise as possible.

Python Statement
Instructions that a Python interpreter can execute are called statements. For example, a = 1 is an assignment statement. if statement, for statement, while statement, etc. are other kinds of statements which will be discussed later.

Multi-line statement
In Python, the end of a statement is marked by a newline character. But we can make a statement extend over multiple lines with the line continuation character (\). For example:
a = 1 + 2 + 3 + \
 4 + 5 + 6 + \ 
 7 + 8 + 9

This is an explicit line continuation. In Python, line continuation is implied inside parentheses ( ), brackets [ ], and braces { }. 
For instance, we can implement the above multi-line statement as:
a = (1 + 2 + 3 +
 4 + 5 + 6 +
 7 + 8 + 9)
We can also put multiple statements in a single line using semicolons, as follows:
a = 1; b = 2; c = 3

Simple programs
Now its time to start writing simple programs using basic Python statements, library functions and modules .
You can use linux/windows for the program development.Choose linux as your favourite operating system always.
For writing simple programs, use IDLE in linux/windows.Open a file and type the program .Save the file with extension '.py' eg: test.py. From the run menu click run module (F5) to run the program.

If you are running the program from command prompt type 
$python test.py.
If you want to run the script from >>>(chevron) prompt type 
>>>import test.py
Note: For advanced programs ( machine learning programs) we will use Jupyter note book or google colab.

When choosing a Python IDE (Integrated Development Environment) for development, you can consider things like its features, whether it's open source, and if it's good for beginners:
  • PyCharm: A full-featured IDE that's good for beginners, intermediate, and advanced coders. It supports Python development, source control, and projects, and includes features like code analysis and a graphical debugger.
  • Spyder: A free, open-source IDE that's often used for scientific development. It integrates well with Python data science libraries.
  • Thonny: A free, open-source IDE with an educational focus that's aimed at beginners.
  • Wing: A popular IDE with features like syntax highlighting, code auto-completion, and a debugger.
  • PyDev: A free, open-source plugin that lets you use Eclipse as a Python IDE.
  • Visual Studio Code: A free, open-source IDE from Microsoft

Program 1:Program to find Area and Circumference of a Circle
#Python Program to find Area and Circumference of a Circle
#Standard formula to calculate the Area of a circle is: a=π r².
#Circumference c=2 π r.
import math
r=input("Enter radius :")
r=int(r)
a=math.pi * r * r
c=2* math.pi * r
print("Area of the circle",a)
print ("Circumference of the circle",c)

Program 2:Input a time in seconds and print the time in HH:MM:SS format (university question)
#proram to convert time in sec to HH:MM:SS format
time=input("Enter time in seconds")
time=int(time)
timeinmin=time//60
timeinsec=time%60
timeinhr=timeinmin//60
timeinmin=timeinmin%60
print("HH:MM::SS----{}:{}:{}".format(timeinhr,timeinmin,timeinsec))
 
Write a program to convert temperature in degree Fahrenheit to Celsius. ((farenheit-32) *5/9= Celsius))
tf= float(input('Enter the temperature in Farenheit:'))
tc=(tf-32)*5/9
print("Temperature in celsius %0.3f"% tc)

Comments

Post a Comment

Popular posts from this blog

Python For Machine Learning - CST 283 - KTU Minor Notes- Dr Binu V P

46.Classes and Objects in Python- Accessors and mutators

KTU Python for machine learning Sample Question Paper and Answer Key Dec 2020