0% found this document useful (0 votes)
3 views

lecture 5

The document explains key concepts in Python programming, including statements, expressions, input/output functions, comments, docstrings, and the use of indentation for defining code blocks. It provides examples for each concept to illustrate their usage. Overall, it serves as a basic guide for understanding Python syntax and structure.

Uploaded by

vishal10thakwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lecture 5

The document explains key concepts in Python programming, including statements, expressions, input/output functions, comments, docstrings, and the use of indentation for defining code blocks. It provides examples for each concept to illustrate their usage. Overall, it serves as a basic guide for understanding Python syntax and structure.

Uploaded by

vishal10thakwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Statement

 Instructions that a Python interpreter can executes are


called statements.
 A statement is a unit of code like creating a variable or
displaying a value.
 >>> n = 17
 >>> print(n)
 Here, The first line is an assignment statement that gives
a value to n.
 The second line is a print statement that displays the
value of n.
Expressions
 An expression is a combination of values, variables, and
operators.
 A value all by itself is considered an expression, and also a
variable.
 So the following are all legal expressions: -
 >>> 42
 42
 >>> a=2
 >>> a+3+2
 7
 >>> z=("hi"+"friend")
 >>> print(z)
 hifriend
Output
Input and output

INPUT
 Input is data entered by user (end user) in
the program.
 In python, input () function is available for
input.
 Syntax for input() is: variable = input
(“data”)
Example

 >>> x=input("enter the name:")


 enter the name: George
 >>>y=int(input("enter the number"))
 enter the number 3
 # python accepts string as default data
type. conversion is required for type.
Output

Output can be displayed to the


user using Print statement.
Syntax:
print(expression/constant/variable)
.
Example
Comments

 A hash sign (#) is the beginning of a comment.


 Anything written after # in a line is ignored by
interpreter.
 Eg:percentage = (minute * 100) / 60
 # calculating percentage of an hour
 Python does not have multiple-line commenting
feature. You have to comment each line
individually as follows:-
Example

# This is a comment.
 # This is a comment,
too.
 # I said that already.
DOCSTRING

 Docstring is short for documentation string.


 It is a string that occurs as the first statement in a
module, function, class, or method definition.
 We must write what a function/class does in the
docstring.
 Triple quotes are used while writing docstrings.
 Syntax:
 functionname__doc.__
Example

 def double(num):
 """Function to double the value"""
 return 2*num
 >>> print(double.__doc__)
 Function to double the value
LINES AND INDENTATION

 Most of the programming languages like C, C++,


Java use braces { } to define a block of code.
 But, python uses indentation.
 Blocks of code are denoted by line indentation.
 It is a space given to the block of codes for class
and function definitions or flow control.
Example

 a=3
 b=1
 if a>b:
 print("a is greater")
 else:
 print("b is greater")

You might also like