Professional Documents
Culture Documents
Python supports the following literals: o Python dictionary stores the data in the key-value
pair.
1. String literals:
o It is enclosed by curly-braces {} and each pair is
separated by the commas(,).
String literals can be formed by enclosing a text in the
quotes. We can use both single as well as double quotes to
1. dict = {'name': 'Pater', 'Age':18,'Roll_nu':101}
create a string.
2. print(dict)
1. Ex "Aman" , '12345'
Tuple:
Numeric literals:
Boolean literals:
1. tup = (10,20,"Dev",[2,3,4])
2. print(tup)
A Boolean literal can have any of the two values: True or
False.
Set:
1. val1=10
2. val2=None
3. print(val1)
4. print(val2)
V. Literal Collections.
List:
Python's for loop is designed to repeatedly execute a code Difference between break and
block while iterating through a list, tuple, dictionary, or
other iterable objects of Python. The process of traversing a continue.
sequence is known as iteration.
Break;
1 The break statement is used to jump out of a loop.
Syntax of the for Loop 2.Its syntax is -:break; 3. The break is a keyword
present in the language 4. It can be used with loops
ex-: for loop, while loop. 5. The break statement is
1. for value in sequence: also used in switch statements
2. { code block }
Continue:- 1 The continue statement is used to
Ex. numbers = [4, 2, 6, 7, 3, 5, 8, 10, 6, 1, 9, 2] skip an iteration from a loop. 2 Its syntax is
3. -:continue; 3 continue is a keyword present in the
4. # variable to store the square of the number language. 4 It can be used with loops ex-: for loop,
5. square = 0 while loop. 5 We can use continue with a switch
6. statement to skip a case.
7. # Creating an empty list
8. squares = []
9.
10. # Creating a for loop
11. for value in numbers:
Defining a Function Features of Python Language
You can define functions to provide the required In this section, we will learn about the different features
functionality. Here are simple rules to define a function of Python language.
in Python.
Interpreted
**Function blocks begin with the keyword def followed
by the function name and parentheses ( ( ) ). Python is processed at runtime using the interpreter.
**Any input parameters or arguments should be placed There is no need to compile program before execution.
within these parentheses. You can also define It is similar to PERL and PHP.
parameters inside these parentheses.
Object-Oriented
**The first statement of a function can be an optional
statement - the documentation string of the function Python follows object-oriented style and design
or docstring. patterns. It includes class definition with various
**The code block within every function starts with a features like encapsulation, polymorphism and many
colon (:) and is indented. more.
**The statement return [expression] exits a function,
optionally passing back an expression to the caller. A Portable
return statement with no arguments is the same as
return None. Python code written in Windows operating system and
can be used in Mac operating system. The code can be
reused and portable as per the requirements.
Syntax
Easy to code
def functionname( parameters ): Python syntax is easy to understand and code. Any
"function_docstring" developer can understand the syntax of Python within
function_suite few hours. Python can be described as “programmer-
return [expression] friendly”
Extensible
Default arguments
If needed, a user can write some of Python code in C
language as well. It is also possible to put python code
A default argument is an argument that assumes a in source code in different languages like C++. This
default value if a value is not provided in the function makes Python an extensible language.
call for that argument. The following example gives an
idea on default arguments, it prints default age if it is
not passed −