Podcast
Questions and Answers
Which of the following is NOT considered a literal in Python?
Which of the following is NOT considered a literal in Python?
What will be the result of the variable assignment name = 'Alice'
?
What will be the result of the variable assignment name = 'Alice'
?
Which of the following correctly describes a tuple in Python?
Which of the following correctly describes a tuple in Python?
Which of the following statements is correct regarding Python keywords?
Which of the following statements is correct regarding Python keywords?
Signup and view all the answers
In Python, how is a comment indicated?
In Python, how is a comment indicated?
Signup and view all the answers
Which of the following variable names is valid in Python?
Which of the following variable names is valid in Python?
Signup and view all the answers
What type of data does the variable b = [2, 9, 4]
represent?
What type of data does the variable b = [2, 9, 4]
represent?
Signup and view all the answers
Which of the following is true about the statement if x < 10:
?
Which of the following is true about the statement if x < 10:
?
Signup and view all the answers
What symbol is used in Python to indicate that a physical line should continue onto the next line?
What symbol is used in Python to indicate that a physical line should continue onto the next line?
Signup and view all the answers
Which of the following correctly suppresses the newline character at the end of a print statement?
Which of the following correctly suppresses the newline character at the end of a print statement?
Signup and view all the answers
What is the purpose of a continuation line in Python?
What is the purpose of a continuation line in Python?
Signup and view all the answers
What must be done to properly display multiple strings on the same line in a print statement?
What must be done to properly display multiple strings on the same line in a print statement?
Signup and view all the answers
Which of the following statements is true about indentation in continuation lines?
Which of the following statements is true about indentation in continuation lines?
Signup and view all the answers
What will be the output of the following code: print('Hello' + 'World')
?
What will be the output of the following code: print('Hello' + 'World')
?
Signup and view all the answers
What does the format code %d
represent in the context of the print function?
What does the format code %d
represent in the context of the print function?
Signup and view all the answers
What would be the result of executing: print('Value:', 'API', 1, 'or', '0')
?
What would be the result of executing: print('Value:', 'API', 1, 'or', '0')
?
Signup and view all the answers
What will happen if a string is passed to the %d
format code?
What will happen if a string is passed to the %d
format code?
Signup and view all the answers
How can you display two strings separated by a space using concatenation?
How can you display two strings separated by a space using concatenation?
Signup and view all the answers
Which of the following format codes is used for displaying floating-point numbers?
Which of the following format codes is used for displaying floating-point numbers?
Signup and view all the answers
What will be the output of executing: print ('Length is %d and Breadth is %d' %(8, 5))
?
What will be the output of executing: print ('Length is %d and Breadth is %d' %(8, 5))
?
Signup and view all the answers
What is incorrect about this print statement? print('Hello World!' + 'It might rain today')
What is incorrect about this print statement? print('Hello World!' + 'It might rain today')
Signup and view all the answers
Study Notes
Literals in Python
- Python has different literals, these are values that are directly represented in the code
- Integer literal is a whole number, example
10
- Floating-point literal is a decimal number, example
10.50
- Imaginary literal is a complex number, example
10.50j
- String literal is a sequence of characters, enclosed in single or double quotes, example
'Hello'
or"World!"
- Triple-quoted strings are used for multi-line strings and can span multiple lines, example
'''Hello World!It might rain today\n# Triple-quoted string literal\nTomorrow is Sunday'''
Variables in Python
- Variables are used to store data in a program
- To set a variable, use the equals sign (
=
) followed by the data it stores - Variable names can consist of letters, numbers, and underscores
- Example
l = 10
,length = 10
,length_rectangle = 10.0
,k="Hello World!"
- Different data types are stored in variables:
- Integer: Example
l = 10
,length = 10
- Floating-point: Example
length_rectangle = 10.0
- String: Example
k="Hello World!"
- Boolean: Example
a=True
- List: Example
b=[2,9,4]
- A list is an ordered, mutable sequence of elements - Tuple: Example
c=('apple', 'mango', 'banana')
- A tuple is an ordered, immutable sequence of elements
- Integer: Example
Keywords in Python
- Keywords are reserved words used for special purposes
- Consist of lowercase letters only
- Cannot be used as identifiers
- Python has 30 keywords:
-
and
,assert
,break
,class
,continue
,def
,del
,elif
,else
,except
,exec
,finally
,for
,from
,global
,if
,import
,in
,is
,lambda
,nonlocal
,not
,or
,pass
,raise
,return
,try
,while
,with
,yield
-
Comments in Python
- Comments are for documentation purposes
- Ignored by the interpreter
- Start a comment with the hash sign (
#
) - All characters after the
#
until the end of the line are part of the comment - Example:
-
# This program computes area of rectangle
-
a=b+c # values of b and c are added and stored in a
-
Continuation Lines in Python
- A physical line is what you see in the code
- A logical line is a single Python statement
- The end of a physical line usually marks the end of a statement
- To continue a statement on the next line, use a backslash (
\
) at the end of the first line - Example:
-
print('This is a long statement that' \
- 'continues on the next line.')`
-
- Python also allows implicit line continuation if an opening parenthesis (
(
), bracket ([
), or brace ({
) is not closed on the same line.
Printing using the print()
function
- The
print()
function is used for displaying messages and results - Syntax:
print(["message"][variable list])
- Example:
-
print("Hello World!")
-
print(10)
-
print(1)
-
print("Length is ", 1)
-
- By default,
print()
adds a newline character at the end - Use
end=''
to suppress the newline character and add a space instead - Example:
-
print("Hello World!", end=" ")
-
print('It might rain today')
-
- Can concatenate strings with
+
or,
- Format codes (
%
) can be used to insert values in a string:-
%s
for strings -
%d
for integers -
%e
for exponential format -
%f
for floating-point format -
%o
for octal format -
%x
for hexadecimal format -
%c
for ASCII code
-
- Example:
-
print ("Length is %d and Breadth is %d" %(l,b))
-
- Automation conversion takes place if the data type doesn't match the format code
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz covers the fundamental concepts of literals and variables in Python programming. Explore different types of literals such as integers, floating-point numbers, and strings, along with how to define and use variables. Test your knowledge on how these elements are utilized within code.