1. COMMON BUILT-IN DATA TYPES IN PYTHON:
There are several built-in data types in Python. Python provides type() and isinstance()
functions to check the type of these variables. These data types can be grouped into the
following categories-
None – None keyword represents the null keyword in Python.
Numeric – There are three distinct numeric types – integers, floating point numbers, complex
numbers. In addition Booleans are subtype of integers.
• int – Stores integer literals including hex, octal and binary numbers as integers. • float - Stores
literals containing decimal values and/or exponent sign as floating-point numbers. • complex –
Stores complex numbers in the form (A + Bj) and has attributes: real and imag. • bool – Stores
boolean value (True or False).
Sequence – According to Python there are four basic Sequence Types – lists, tuples, and
range.
• list – Lists are mutable sequences, typically used to store collections of homogeneous items. •
tuples – Tuples are immutable sequences, typically used to store collections of heterogeneous
data. • range – The range type represents an immutable sequence of numbers and is commonly
used for looping a specific number of times in for loops. • string – Immutable sequence of
characters to store textual data.
Mapping – Mapping objects are mutable and there is currently only one standard mapping type,
the dictionary.
• dict – Mutable unordered collection of items.
Set – Python has two built-in set types – set and frozenset.
• set – Mutable unordered collection of distinct hashable objects. • frozenset – Immutable
collection of distinct hashable objects.
2. CONTROL FLOW TOOLS IN PYTHON:
2.1 if statements
➢ The if statement is used in Python for decision making. ➢ It contains a body of
code which runs only when the condition given in the if statement is TRUE. ➢ If the
condition is FALSE, then the optional else statement runs which contains some code
for the else condition. ➢ The keyword ‘elif’ is short for ‘else if’ and is useful to avoid
excessive indentation.
2.2 for Statements
➢ It has an ability to iterate over the items of any sequence, such as a list or a
string, in order that they appear in the sequence. ➢ The for statement in Python
differs a bit from what you may be used to in C or Pascal.
2.3 The range () Function
➢ If you do need to iterate over a sequence of numbers, the built-in function range
() comes in handy. It generates arithmetic progressions. ➢ The given end point is
never part of the generated sequence. ➢ Example: range (5,10) Output: 5,6,7,8,9
2.4 Break and Continue statements
,Break Statement –
➢ The Break statement, like in C, breaks out of the innermost enclosing for or while
loop. ➢ The Break statement terminates the loop immediately, and control flows to
the statement after the body of the loop.
Continue Statement –
➢ The Continue statement, also borrowed from C, continues with the next iteration
of the loop. ➢ The Continue statement terminates the current iteration of the
statement, skips the rest of the code in the current iteration and control flows to the
next iteration of the loop.
2.5 Pass Statements
➢ The Pass statement does nothing. It can be used when the statement is required
syntactically but the program requires no action. ➢ Pass keyword in Python is
generally used to fill-up empty blocks and is similar to an empty statement
represented by semi-colon in languages such as Java, C++.
2.6 Defining Functions
The keyword def introduces a function definition. It must be followed by the function name and
the parenthesized list of formal parameters.
➢ The statements that form the body of the function start at the next line, and must
be intended.
Example: def fib(n):
#write Fibonacci series upto n
a,b=0,1
while a < n:
print(a, end=’ ‘)
a , b = b , a+b
print()
#Now call the function we just defined
fib(2000)
Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597
2.7 More on Defining Functions
2.7.1 Default Argument Values
➢ The most useful form is to specify a default value for one or more arguments. ➢
This creates a function that can be called with fewer arguments than it is defined to
allow. ➢ Example: def greet(name, msg=’Good morning!’):
print(“Hello”, name + ‘ , ‘ + msg)
greet (“Kate”)
greet(“Bruce”, ”How do you do!”)
Output: Hello Kate, Good morning!
Hello Bruce, How do you do?
2.7.2 Keyword Arguments
➢ Functions can also be called using keyword arguments of the form
kwarg = value.
, ➢ Example: def SomeFun (**kwargs):
for argumentname, argumentvalue in kwargs.items()
print(argumentname, argumentvalue)
SomeFun(name = ‘Kiran’ ,age = 23, role = ‘Engineer’)
Output: name = Kiran age = 23 role = Engineer
2.7.3 Special Parameters
➢ By default, arguments may be passed to a Python function either by position or
explicitly by keyword. ➢ For readability and performance, it makes sense to restrict
the way arguments can be passed so that a developer need only look at the
function definition tp determine if items are passed by position, by position or
keyword, or by keyword. • Positional – or – Keyword Arguments If / and * are not
present in the function definition, arguments may be passed to a function by
position or by keyword.
• Positional – Only Parameters It is possible to mark certain parameters as positional – only.
If positional – only, the parameters order matters, and the parameters cannot be passed by
keyword.
Positional – only parameters are placed before a / (forward slash).
• Keyword – Only Arguments To mark parameters as keyword – only, indicating the
parameters must be passed by keyword arguments, place an * in the arguments list just before
the first keyword – only parameters.
Example: def standard_arg (arg):
print(arg)
def pos_only_arg (arg, /):
print(arg)
def kwd_only_arg (*, arg)
print(arg)
2.7.4 Arbitrary Argument Lists
➢ The least frequency used open is to specify that a function can be called with an
arbitrary number of arguments. ➢ These arguments will be wrapped up in a tuple.
Before the variable number of arguments, zero or more normal arguments may
occur. ➢ Example: def concat (*args, sep = “ / “): return sep.join(args)
concat(“earth” , “mars” , “venus”) Output: ‘earth/mars/venus’
2.7.5 Unpacking Argument Lists
➢ The reverse situation occurs when the arguments are already in a list or tuple but
need to be unpacked for a function call requiring separate positional arguments. ➢
For instance, the built – in range() function expects separate start and stop
arguments. ➢ If they are not available separately, write the function call with the *
operator to unpack the arguments out of the list or tuple. ➢ Example:
list(range(3,6)) #normal call with separate arguments Output: [3,4,5]
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller bhagyeshbagde. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $9.77. You're not tied to anything after your purchase.