Learning Python- Basic course: Day 2, Statements, Comments and Indentation

Aatmaj - Jun 20 '21 - - Dev Community

✌️Hi all! Welcome to the second day of the course! Today let us learn about some Python syntax.


Statements in Python
Statements in Python are of three types
a) Expressions- Statements which perform actions like addition, division, or any other such operations.
Here are a few sample commands.
image

b) Assignments- The statements giving values to variables are called assignments.
image

c) Declarations - Functions, classes in python are declared by using the keyword "def". The function declaration must end with semicolon ":"
image

Indentation in Python- We need to leave spaces after every loops and function declarations. Brackets in Java are replaced by indentation in Python.
If Indentation is not respected, this error is generated.
image
Moreover, every loop must end by a semicolon ':'

Comments in Python - Comments in Python begin with a '#' These commands are ignored by the interpreter and used to make the program clearer for the developers.

image

Comments are an essential part of documentation. Proper documentation is a good programming habit. More about it here.


Now, let us see a sample Python program-

print("Program to accept student details.") 
#here, print() takes one string argument.

#input()  takes one string argument which it prints out.
#input()  returns one string argument
Rollno=int(input("Enter Roll Number ")) 
#int() converts the string into an integer type

name=input("Please enter a name ")

avg=float(input("enter percentage scored "))
#float() converts the string into an float type

print("Rollno of the student ",Rollno)
print("Name of the student ",name)
print("Percentage scored",avg,"%")
#here print() takes multiple arguments which it prints in the sequential order

Enter fullscreen mode Exit fullscreen mode

Output-
image

Note, that as we need to operate upon the numbers inputted, we have used the int() method.
But if we input a character or string in place of the int, we will get the following error.
image

In order to prevent such errors, error handling must be dont, which will be covered in this course...

Here is another program which gives the value of net resistance in parallel

a=int(input("Please enter first resistance "))
b=int(input("Please enter second resistance "))
c=(a*b)/(a+b)
#write the brackets carefully
print("answer is",c)
Enter fullscreen mode Exit fullscreen mode

output-image
More about operators in Python will be in the next blog...

Exercise----
1) Write a code in Python to find average of four numbers in one line, without declaring any variables.
2) Write a code to reverse a three digit number in Python.
(Answers can be found here)

To be continued....😏


So friends that's all for this part. 😊 Hope you all are enjoying.😎 Please let me know in the comment section if you liked it or not. 🧐 And don't forget to like the post if you did. 😍 I am open to any suggestions or doubts. 🤠 Just post in the comments below or gmail me. 😉
Thank you for being so patient.👍

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .