[go: up one dir, main page]

0% found this document useful (0 votes)
3 views6 pages

1.3-Comments, Identifiers and Keywords

The document provides an overview of comments, input-output functions, output formatting, and basic concepts in Python programming. It explains how to use comments, the input() and print() functions, and the eval() function for evaluating expressions. Additionally, it covers indentation, tokens, variables, identifiers, and keywords in Python, including rules for naming identifiers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

1.3-Comments, Identifiers and Keywords

The document provides an overview of comments, input-output functions, output formatting, and basic concepts in Python programming. It explains how to use comments, the input() and print() functions, and the eval() function for evaluating expressions. Additionally, it covers indentation, tokens, variables, identifiers, and keywords in Python, including rules for naming identifiers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Comments in Python

❖ A comment statement contains information for persons reading the program.


Comment statements are non-executable statements so they are ignored during
program execution. They have no effect on the program results.

❖ In python comments begin with hash symbol (#).

❖ The lines that begins with # symbol are considered as comments and ignored by the
python interpreter.

❖ For multiline comments three single quotes(‘’’) or three double quotes (“””) can be
used

Example 1
print("Sri Eshwar") #This is a comment
Example 2
#This is a comment
#written in
#more than just one line
print("Sri Eshwar")
Example 3
"""
This is a comment
written in
more than just one line
"""
print("Sri Eshwar")
Example 4
‘’’
This is a comment
written in
more than just one line
‘’’
print("Sri Eshwar")
Input and Output Functions
❖ A program needs to interact with the user to accomplish the desired task; this can be
achieved using Input-Output functions.

❖ The input () function helps to enter data at run time by the user and the output function

print () is used to display the result of the program on the screen after execution. ❖ In
python print () function is used to display the result on the screen.
The syntax for print () is as follows:
print(“String to be displayed as output”)
print(variable)

print(“String to be displayed as output”, variable)


print (“String1 ”, variable, “String 2”, variable, “String 3” ……)

o Example
>>> print("Welcome to Python Programming")
Output
Welcome to Python Programming
Script
>>> x=5
>>> y=6
>>> z=x+y
>>> print(z)
Output
11
Script
>>> print("The sum=",z)
Output
The sum= 11
Script
>>> print("The sum of",x,"and",y,"is",z)
Output
The sum of 5 and 6 is 11

Output Formatting
o Sometimes we would like to format out output to make it look attractive.
o This can be done by using the str.format() method
Example Output
>>> x=10;y=20 The values of x is 10 and
>>> print('The values of x is {} and y is {}'.format(x,y)) y is 20

>>> print('I am studying {0} and {1}'.format('BE','CSE')) I am studying BE and CSE

>>> print('Hello {a},{b}'.format(b='good morning',a='Muni')) Hello Muni,good morning

>>> x=123.456789 The value of x is 123.46


>>> print('The value of x is %.2f'%x)

>>> name='Muni' Muni is 40 years old.


>>> age=40
>>> print("%s is %s years old."%(name,age))

>>> price=150 The Apple costs 150 rupees


>>> fruit='Apple'
>>> print("The %s costs %d rupees"%(fruit,price))

The input() function


∙ The input () function is used to accept an input from a user.

∙ A programmer can ask a user to input a value by making use of input().

❖ Variable = input (“prompt string”)

∙ Example

❖ >>>x=input(“Enter the name:”)

❖ Enter the name: bala

❖ >>>y=int(input(“Enter the number”))

❖ Enter the number 3

Note: Python accepts string as default type. Conversion is required for type.

The eval() FUNCTION


❖ The full form of eval function is to evaluate.
❖ It takes string as a parameter and returns it as if it is a python expression.

The eval function takes a string and returns it in the type it is expected. The
following example illustrates this concept
Example
>>> X=eval('123')
>>> X
123
>>> type(X)
<class 'int'>

❖ By making use of eval () function, we can avoid specifying a particular

type in front of input () function.


Program to Demonstrate the use of Output
eval function

Name=(input('Enter Name:')) Enter Name: Pranav


Age=eval(input('Enter Age:')) Enter Age: 40
Gender=(input('Enter gender:')) Enter gender: M
Height=eval(input('Enter Height:')) Enter Height:5.6
print('User Details are as follows:') User Details are as follows:
print('Name :',Name) Name : Pranav
print('Age:',Age) Age: 40
print('Gender:',Gender) Gender: M
print('Height:',Height) Height: 5.6
print(type(Age)) <class 'int'>

Indentation
❖ Python uses whitespace such as spaces and tabs to define program blocks whereas other languages
like C, C++, java use curly braces { } to indicate blocks of codes for class, functions or body of the
loops and block of selection command.

❖ The number of whitespaces (spaces and tabs) in the indentation is not fixed, but all statements

within the block must be indented with same amount spaces.


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

Tokens
❖ A token is the smallest unit of the program.

❖ The normal token types are:

o Identifiers/Variables
o Keywords
o Operators
o Delimiters
o Literals

Variable and Identifiers

❖ Variables in simple language, means its value can vary.

❖ Variables are noting but just parts of computer’s memory where information is stored.

❖ To be identified easily, each variable is given an appropriate name.

❖ Variables are examples of identifiers.

❖ An Identifier is a name used to identify a variable, function, class, module or object.

Rules
o An identifier must start with an alphabet (A..Z or a..z) or underscore ( _ ).
o Identifiers may contain digits (0 .. 9), but cannot start with a digit.
o Python identifiers are case sensitive i.e. uppercase and lowercase letters are distinct
o Identifiers must not be a python keyword.
o Python does not allow punctuation character such as %,$, @ etc., within identifiers.
⮚ Example of valid identifiers
o Sum, total_marks, regno, num1
⮚ Example of invalid identifiers
o 12Name, name$, total-mark, continue
⮚ Assigning a value to a variable
o In Python, the equal sign (=) is used as the assignment operator
o Syntax : Variable=expression

Keywords
⮚ Keywords are the reserved words in Python.
⮚ Keywords are case sensitive.
⮚ We cannot use a keyword as variable name, function name or any other identifier.
and assert break class continue def del elif else Except

exec finally for from global if import in is Lambda

not or pass print raise return try while with yield

You might also like