Python Data Types List In Python, list is a type of container in Data Structures, which is used to store multiple data at the same time. Lists in Python can be created by just placing the sequence inside the square brackets[]. List is mutable # Creating a List List = [] print ( "Initial blank List: " ) print (List) # Creating a List with # the use of a String List = [ 'hello world' ] print ( " \n List with the use of String: " ) print (List) # Creating a List with # the use of multiple values List = [ "hello" , "world" ,] print ( " \n List containing multiple values: " ) print (List[ 0 ]) print (List[ 1 ]) #Creating a Multi-Dimensional List #(By Nesting a list inside a List) List = [[ 'hello' , 'world' ] , [ 'hello' ]] print ( " \n Multi-Dimensional List: " ) print (List) # Creating a
Posts
- Get link
- Other Apps
PYTHON ARRAY Array can be handled in python by module named “ array “. They can be useful when we have to manipulate only a specific data type values. Program 1 array(),append(),insert() # array(), append(), insert() # importing "array" for array operations import array # initializing array with array values # initializes array with signed integers #Here i is signed integers arr = array.array( 'i' , [ 1 , 2 , 3 ]) print ( "The new created array is : " , end = " " ) for i in range ( 0 , 3 ): print (arr[i], end = " " ) print ( "" ) # using append() to insert new value at end arr.append( 4 ); print ( "The appended array is : " , end = "" ) for i in range ( 0 , 4 ): print (arr[i], end = " " ) # using insert() to insert value at specific position # inserts 3 at 1st position arr.insert( 1 , 3 ) pri
Python Loops And Statements
- Get link
- Other Apps
while loop when condition of given statements is True, while loop is execute. Example: i = 0 while i < 5: print(i) i += 1 Output: 0 1 2 3 4 Break and Continue Statements *In Python, break and continue statements can alter the flow of a normal loop. *Loops iterate over a block of code until test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression. *The break and continue statements are used in these cases. While loop with break Statements: Example: i = 0 while i < 5: print(i) if i == 3: #break the loop break i +=1 Output: 0 1 2 3 While loop with continue Statements: Example: i = 0 while i < 5: i += 1 if i == 3: #continue the loop continue print(i) Output: 1 2 4 5 For Loop for loop we can execute a set of statements, once for each item in a list, tuple, set etc. Examp
Python Conditions
- Get link
- Other Apps
Python Conditions and if Statements Equals: a == b Not Equals: a != b Less than: a < b Less than or equal to: a <= b Greater than: a > b Greater than or equal to: a >= b if Statements If is a keyword for conditional statements. Example: a = 33 b = 200 if b > a: print("b is greater than a") Output: b is greater than a Example: a = 33 b = 200 if b > a: print("b is greater than a") Output: Error *** Indentation Error In case of other programming language, there is a curly brackets, but in python it is different, i.e. white space. Elif statements If the previous statements were not true, try this condition. Example: a = 33 b = 33 if b > a: print("b is greater than a") #this condition is not true. elif a == b: print("a and b are equal") Output:
Basic Operators in Python
- Get link
- Other Apps
Operators in Python · Arithmetic operators · Relational operators · Logical operators · Bitwise operators · Assignment operators · Special operators Arithmetic operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division. Example: x = 10 y = 4 # Addition of numbers add = x + y # Subtraction of numbers sub = x - y # Multiplication of number mul = x * y # Division(float) of number div1 = x / y # Division(floor) of number div2 = x // y # Modulo of both number mod = x % y # print results print(add) print(sub) print(mul) print(div1) print(div2) print(mod) output: 14 6 40 2.5 2 2 Relational Operators It compares the values. It either returns True or False according to the given condition. Example: a = 55 b = 33 # a &g