0% found this document useful (0 votes)
8 views13 pages

Python Statements and Expressions Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Python Statements and Expressions Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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")

Common questions

Powered by AI

Docstrings in Python are used for documenting what a module, class, or function does. They provide a convenient way of associating documentation with Python code and can be accessed using the __doc__ attribute. The benefit of docstrings is that they offer a standardized way to document code with clarity. An example implementation is defining a function with a docstring: 'def double(num): """Function to double the value""" return 2*num'. This allows the docstring to be printed using 'print(double.__doc__)', which outputs "Function to double the value" .

In Python, a statement is a unit of code that the interpreter can execute, such as an assignment or print function. An expression, on the other hand, is a combination of values, variables, and operators that evaluates to a value. Every expression is legal code and can be considered a statement if it stands alone but not vice versa; for example, 'a = 2' is a statement, while 'a + 3 + 2' is an expression that evaluates to 7 .

Python uses indentation to define blocks of code, which is different from languages like C or Java that use braces { } for this purpose. Indentation in Python is crucial because it determines the grouping of statements, which affects control flow and scope within the program. This can lead to cleaner and more readable code compared to other languages that use braces, but it also requires careful attention to formatting .

Comments in Python, beginning with the # character, allow developers to add explanatory notes within the code; however, they are limited to single lines. Python lacks a native multi-line comment feature, necessitating each line to begin with #. In contrast, Python's docstrings are multi-line and offer a structured way to document a module, function, class, or method. They provide a description at the beginning of definitions in triple quotes, which can subsequently be accessed using the __doc__ attribute, making them more suitable for comprehensive documentation .

The print statement in Python is used to output expressions, constants, or variables to the console. Its syntax is 'print(expression/constant/variable)'. For example, the statement 'print("Hello, World!")' would output the string "Hello, World!" to the screen. This function is essential for displaying results to the user in a visible format .

Expressions in Python are formed by combining values, variables, and operators to compute a result. They can be as simple as a single value or variable, like '42' or 'y', or they can be more complex, including operations and function calls. Three examples showing versatility are: (1) a simple arithmetic operation 'a + 3 + 2', which evaluates to 7 if 'a' is 2; (2) a string concatenation '"hi" + "friend"', which evaluates to 'hifriend'; and (3) a function call 'len("Python")', which evaluates to 6 .

In Python, the input function is used to take input from a user with the syntax 'variable = input("prompt")'. By default, the input function returns the user's input as a string. Therefore, type conversion is necessary if a specific data type is required, such as converting a string to an integer using the int() function when numerical computations are needed from the input .

Python uses indentation to define blocks of code instead of braces like many other languages. Each block of code must have consistent indentation, which Python relies on to determine the structure of the program, such as the boundaries of loops and conditional statements. This mechanism is significant for readability, as it enforces a uniform appearance across Python code, allowing more intuitive understanding and maintenance of the code structure .

The Python interpreter ignores any text in a line following the hash mark (#), treating it as a comment. This allows developers to include explanatory text within their code. However, Python does not natively support multi-line comments; developers must begin each line of a multi-line comment with a hash mark. This can be cumbersome compared to languages with dedicated multi-line comment syntax, such as C++ or Java, which use /* to */ blocks, allowing for more flexible and extensive commenting .

A Python statement is a single line of code that performs an action. For example, an assignment statement like 'n = 17' assigns the integer value 17 to the variable 'n'. Another example is a print statement, such as 'print(n)', which outputs the value of 'n' to the console. Each statement serves a specific purpose within a program, such as assigning values or producing output, thereby contributing to the overall logic and functionality .

You might also like