0% found this document useful (0 votes)
13 views8 pages

Python Token (2023-24)

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Basic fundamental terms of Python

1. What is comment?

Ans: Comments can be used to explain Python code.


Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.

EX 1: #this is a comment
print ("Hello, World!")

Output: Hello, World!

EX 2:Print ("Hello, World!") #this is a comment

Output: Hello, World!


EX 3:
"""
This is a comment
written in
more than just one line
print ("Hello, World!")”””

No output

2. What is token?
Ans: The smallest individual unit in a program is called as
token or lexical unit. The different types of token are
• Keywords
• Identifiers
• Literals
• Operators
• Punctuators/Delimiters
3. What is identifier?
Ans: A Python identifier is the name given to a variable,
function, class, module or other object. An identifier can begin
with an alphabet (A – Z or a – z), or an underscore (_) and can
include any number of letters, digits, or underscores. Spaces
are not allowed. Python will not accept @, $ and % as
identifiers. Furthermore, Python is a case-sensitive language.
Thus,
Hello and hello both are different identifiers
4. What is keyword?
Keywords are the reserved words in Python used by Python
interpreter to recognize the structure of the program.

5. What is literal?
Ans: Literal are also called constant values which are the data
items having fixed values. Python allows different literal that
are string, numeric, Boolean etc.
Ex: x=23(Here 23 is a numeric literal)
6. What is operator?
Ans: Operators are special symbols which represent
computation. They are applied on operand(s), which can be
values or variables. Same operators can behave differently on
different data types. Operators when applied on operands form
an expression. Operators are categorized as Arithmetic,
Relational, Logical and Assignment. Value and variables when
used with operator are known as operands.
Arithmetic operators

Operator Meaning Expression Result


+ Addition 10 + 20 30
- Subtraction 30 - 10 20
* Multiplication 30 * 100 300
/ Division 30 / 10 3.0
1/2 0.5
// Integer 25 // 10 2
Division
1/2 0
% Remainder 25 % 10 5
** Raised to 3 ** 2 9
power

Relational or Comparison operators


Operator Meaning Expression Result

> Greater Than 20>10 True

15>25 False

< Less Than 20<45 True


20<10 False

== Equal To 5== 5 True


5== 6 False

!= Not Equal to 67!= 45 True


35!= 35 False

>= Greater than 45>= 45 True


or Equal to
23 >= 34 False

<= Less than or 11<=34 True


equal to
11<=11 True
Logical operator

Operator Meaning Expression Result

And
And operator True and True True

True and False False

Or Or operator True or False True

False or False False

Not
Not Operator not False True

not True False

Assignment operators

Operator Expression Equivalent to

= X=5

+= X+=5 X=X+5

-= X-=5 X=X–5

*= X*=5 X=X * 5

/= X/=5 X=X / 5
7. What is a variable?
Ans: Variable is a named memory location.Variables are
containers for storing data values. Python has no command
for declaring a variable. A variable is created the moment you
first assign a value to it.
A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume). Rules for
Python variables:

• A variable name must start with a letter or the underscore


character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three
different variables)(

• Keyword cannot be used as a variable name.

Legal variable names: Illegal variable names:

myvar = "John" 2myvar = "John"(cannot start with a digit)


my_var = "John" my-var = "John"(arithmetic sign is there)
my_var = "John" my var = "John"(space not allowed)
myVar = 6.7 class=”ix” (class is a keyword)
MYVAR = "John"

myvar2 = 78

Examples on Sample Code Output


Variables:
Assigning a value Website = "xyz.com" xyz.com
to a variable print(Website)
Changing value Website = "xyz.com" xyz.com
of a variable print(Website) abc.com
Website = "abc.com"
print(Website)
Assigning a,b,c =5,3.2,"Hello" 5
different values print(a) 3.2
to different print(b) Hello
variables print(c)
Assigning same x=y=z= 5 5
value to different print(x) 5
variable print(y) 5
print(z)

8. What is datatype and what are the different types used in


Python?
Ans:
Every value in Python has a datatype. Since everything is an
object in Python programming, data types are actually classes and
variables are instance (object) of these classes.
There are various data types in Python. Some of the important
types are mentioned below in the image
Python has 7 basic data types which are as follows:
1. Numeric
i. int
ii. long
iii. float
iv. complex
2. string
3. list
4. tuple
5. set
6. dictionary
7. boolean

x = "Hello World" str

x = 20 int

x = 20.5 float

x = 1j complex

x = ["apple", "banana", "cherry"] list


x = ("apple", "banana", "cherry") tuple

x = {"name" : "John", "age" : 36} dict

x = {"apple", "banana", "cherry"} set

x = True bool

8. What is delimiter/punctuator?
Ans: A punctuator is a token that has syntactic and semantic
meaning to the compiler, but the exact significance depends on the
context. The following nine ASCII characters are the separators:
(){}[];,.\#@:=‘“

9. What is escape sequence?


• Ans: An escape sequence is a special character used in the
form of backslash (\) followed by a character that is required.
• These characters are used to represent whitespace.
• Whitespace gives characters like space, tab, form feed, vertical
tab.

Ex:
\n This represents a newline

\t This represents a tab

10. What is type conversion?


Ans: The process of converting the value of one data type
(integer, string, float, etc.) to another data type is called type
conversion. Python has two types of type conversion.
1. Implicit Type Conversion
2. Explicit Type Conversion

Implicit Type Conversion


In Implicit type conversion, Python automatically converts one
data type to another data type. This process doesn't need any
user involvement.
Ex:a=3.5
b=2*a
print(b)
Output
7.0
(Note b automatically becomes a float value)
Explicit Type Conversion
In Explicit Type Conversion, users convert the data type of an
object to required data type. We use the predefined functions like
int(), float(), str(), etc to perform explicit type conversion.
This type of conversion is also called typecasting because the
user casts (changes) the data type of the objects.

• Ex1: a=3.5
b=int(a)+2
print(b)
Output
5
(Note:Here output is not 5.5 because a converted to int so
instead of 3.5 it will take 3)
• Ex2:a=3
b=float(a) * 2
print(b)
Output
6.0
(Note: Instead of 6 the output will be 6.0 because 3 got converted
to 3.0 by typecasting)

You might also like