0% found this document useful (0 votes)
19 views

Bit Py

Uploaded by

Mohammed Razi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Bit Py

Uploaded by

Mohammed Razi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DATATYPES FUNCTIONS are self-contained programs that perform some .

Python Nested if statements Python SetS


The data stored in the memory can .Particular tasks. We can have a if...elif...else statement inside A set is an unordered collection of items. Every set element is unique (no
be of many types.So there are Once a function is created by a programmer for a specific task .another if...elif...else statement. This is called nesting in computer duplicates)and must be immutable (cannot be changed).
6 basic datatypes in Python ,this function can be called anytime to perform that task. programming. Any number of these statements can be nested However, a set itself is mutable. We can add or remove items from it.
1.Numeric Each function is given a name,using which we call it. inside one another. Eg. Sets can also be used to perform mathematical set operations like union,
Numeric data can broadly divided into integers and There are many advantages of using functions: >>>num = float(input("Enter a number: ")) #can you remember intersection,symmetric difference, etc.
real numbers.Integers can be +ve or –ve. *They reduce duplication of code in a program. this function??? if num >= 0: if num == 0: Creating Python Sets
The real numbers are called floating point numbers. *They break the large complex program into small parts. print("Zero") else print("Positive number") A set is created by placing all the items (elements) inside curly braces {},
Floating point numbers contain a decimal and a *They help in improving the clarity of code else print("Negative number") separated by comma, or by using the built-in set() function.
fractional part.Eg.>>>num1=2#integer Users can build their own functions which are called for Loop It can have any number of items and they may be of different types
>>>num2=2.5#real or floating point number User defined functions. The Python for loop is an iterator-based for loop. (integer, float, tuple,string etc.). But a set cannot have mutable elements
>>>num1 2 #output BUILT-IN FUNCTIONS It goes through the elements in any ordered sequence list i.e. like lists, sets or dictionaries as its elements.
>>>num2 2.5 #output Built-in functions are those functions already defined in Python string,lists,tuples, the keys of dictionary and other iterables Removing elements from a set
2.String programming language. range() Function A particular item can be removed from a set using the methods discard()
A string is a series of alphabets,numerals and special We can directly call them to perform a particular task. The range() function is a built-in function in Python that helps us to and remove().The only difference between the two is that the discard()
characters. Single quotes or double quotes are used e.g. the Math module has some mathematical built-in functions iterate over a sequence of numbers. function leaves a set unchanged if the element is not present in the set.
to represent strings.Similar to C the first index is 0. that perform tasks related to Mathematics. while Statement On the other hand, the remove() function will raise an error in such a
There are many operations that can be performed on Mathematical Functions The while statement is used when you have a piece of code and condition (ifelement is not present in the set).
a string.Operators such as slice operator([] and [:]), In Mathematics we have functions such as sin and log and we you want to repeat it ‘n’ number of times or forever. Return Statement in Python
concatenation operator(+),repetition operator(*) etc. have to evaluate some expressions like sin(pi/4) and log(1/x). Nested while loop A return statement is used to end the execution of the function call and
Slicing is used to take out of a subset of a string. Python provides a Math module that contains most of the When a while loop is present inside another while loop then it is “returns” theresult (value of the expression following the return keyword)
Eg.>>>s=“Hello” #store string value familiar and important mathematical functions. called nested while loop to the caller.The statements after the return statements are not executed.
S #display string value Date and Time functions Infinite Loop If the return statement is without any expression, then the special value
‘Hello’ #output Python provides the built-in modules time and calendar through A loop becomes infinite loop if a condition never becomes FALSE. None isreturned.The statement return [expression] exits a function,
3.List which we can handle date and time in several ways. This results in a loop that never ends. Such a loop is called an optionally passing back anexpression to the caller.
List is the most used datatype in Python. For eg. We can use these modules to get the current date and infinite loop. A return statement with no arguments is the same as return None.
A list can contain same or different types of items. time. STRINGS Recursion FUNCTION
A list is an ordered and indexable sequence.To declare In order to use these modules we have to import the A string is a sequence of characters. A character is simply a symbol. Python also accepts function recursion, which means a defined function can
a list in Python we need to separate the itemusing corresponding modules in our program. For example, the English language has 26 characters. In Python, a call itself.Recursion is a common mathematical and programming concept.
commas and enclose them within square brackets([]). User Defined Functions string is a sequence of Unicode characters. Unicode was introduced It means that a function calls itself. This has the benefit of meaning that you
The difference between an array and a list is that an Python allows users to define their own functions. to include every character in all languages and bring uniformity in can loopthrough data to reach a result.
array can contain same type of items while a list can To use their own functions in Python ,users have to define the encoding. Strings can be created by enclosing characters inside a The developer should be very careful with recursion as it can be quite easy
contain different types of items. function first. This is known as function definition. single quote or double-quotes.Even triple quotes can be used in to slip intowriting a function which never terminates, or one that uses
Eg.>>>first=[1,”two”,3.0,”four”] #firstlist Eg;- # Program to illustrate Python but generally used to represent multiline strings and excess amounts of memory or processor power.
>>>second=[“five”,6] #second list # the use of user-defined functions docstrings. Anonymous (Lambda) FunctioN
>>>first #display first list def add_numbers(x,y): OUTPUT Python String Operations;- In Python, anonymous function means that a function is without a name.
[1,’two’,3.0,’four’] #output sum = x + y The sum is 11 1.Concatenation of Two or More Strings,2. Iterating Through a As we already know that def keyword is used to define the normal
4.Tuple return sum string,3. String Membership Test,4. Built-in functions to Work with functions and the lambda keyword is used to create anonymous functions.
Similar to a list tuple is also used to store sequence of num1 = 5 num2 = 6 Python It has the following syntax:lambda arguments: expression
items.Like a list a tuple consists of items seperated by print("The sum is", add_numbers(num1, num2)) Compound Data Type This function can have any number of arguments but only one expression,
commas.Tuple are enclosed within parentheses rather ------------------------------------------------------------------------------------- Strings are very different from int and float data types and these which is evaluated and returned.
than square brackets. Eg.>>>third=(7,”eight”,9,10.0) PARAMETERS AND ARGUMENTS are the values or expressions are made up of smaller pieces/characters.
One is free to use lambda functions wherever function objects are
>>> third (7,’eight’,9,10.0) #output passed to the functions between parentheses. The data types that are made up of smaller pieces are known as required. You need to keep in your knowledge that lambda functions are
5.Dictionary There can be four types of formal arguments using which a compound data types syntactically restricted to a single expression.
A python dictionary is an unordered collection of function can be called which are as follows. String slices-A piece or subset of a string is known as a slice.
key-value pairs.When we have a large amount of data 1.Required Arguments 2.Keyword Arguments Slice operator is applied to a string with the use of square braces
dictionary datatype is used.Keys and values can be of 3.Default Arguments 4.Variable-length Arguments String Traversal
any type in a dictionary.Items in a dictionary are CONTROL STATEMENTS Traversal is process in which we access all elements of the string
enclosed in the {} and seperated by the comma(,). A control statement is a statement that determines whether one by one using some conditional statements such as for loop,
A (:) is used to separate key from value. other statements will be executed while loop etc
A key inside the square bracket [] is used for CONDITIONAL STATEMENTS;-The statement for the decision String formatting operator
accessing dictionary items. Eg:- making are called conditional statements or conditional The strings in Python have a unique built-in operation : the %
.>>>dict1={1:”first”, ”second”:2} #declare dictionary expressions.The actions performed by these conditional operator (modulo).
>>>dict1[3]=“third” #add new item statements are entirely depended on the value of the condition, Elif Statements
>>>dict1 #display dictionary on whether the value is TRUE or FALSE It is used to specify a
{1:”first”, ”second”:2,3:”third”} #output SIMPLE IF STATEMENT;- Here the program evaluates the test new condition to test,
6.Boolean expression and will execute statements only if the test expression if the first condition is false
Sometimes we need to store the data in the form is true.If it is false the statements will not execute.
of ‘Yes’ or ‘No’.Yes is similar to True and No is
similar to false. This True and False data is known
as Boolean data and the data types which stores this
Boolean data are known as Boolean Data Types.
Eg.>>>a=True
>>>type(a)
<type ’bool’>
>>>x=False
>>>type(x)
<type ‘bool’>

You might also like