Subscribe Us

Dictionary And Loops Statement in Python

Dictionary in Python

Dictionaries are used to store data values in key: value pairs, Dictionaries are a fundamental data structure in Python used to store collections of items. Unlike lists or tuples, which are indexed by numerical positions, dictionaries store items in key-value pairs. Keys act as unique identifiers for accessing the corresponding values.

They are unordered, mutable(changeable) & don’t allow duplicate keys.


Dictionary And Loops Statement in Python



dict = {

"name": "rahul"

"pet": 10.6

"mark" : [ 83, 94, 96 ]

}


dict[”name”], dict[”pet”], dict[”marks”] “key” : value dict[”key”] = “value” - to assign or add new

 

Dictionary Methods in Python- 


Python dictionaries are powerful collections that store key-value pairs, making them essential for various tasks. Here's a comprehensive overview of commonly used dictionary methods to effectively manage and manipulate data within them.

 

  • myDict.keys( )  - returns all keys 
  • myDict.values( )  - returns all values 
  • myDict.items( )  - returns all (key, val) pairs as tuples 
  • myDict.get( “key““ )  - returns the key according to value 
  • myDict.update( newDict )  - inserts the specified items to the dictionary

 

Set in Python in Python


Id be glad to provide a comprehensive response on Sets in Python:

Sets in Python

  • Sets are unordered, unchangeable collections of unique elements. They are similar to lists in terms of storing elements, but they offer distinct characteristics:
  • Uniqueness: Sets cannot contain duplicate elements. If you try to add a duplicate, it will be silently ignored.
  • Unordered: Elements in a set are not stored in a specific order. The order you see when printing a set may not be the same when you print it again.
  • Mutability: Sets themselves are mutable (you can add or remove elements), but the elements within a set must be immutable (e.g., strings, integers, tuples).


The set is a collection of unordered items. Each element in the set must be unique & immutable. 

nums = { 1, 2, 3, 4 } 

set2 = { 1, 2, 2, 2 } - repeated elements stored only once, so it resolved to {1, 2} 


null_set = set( ) - empty set syntax

 

 

Set Methods in Python -

 

  • set.add( el )  - adds an element
  • set.remove( el )  - removes the elem an 
  • set.clear( )  - empties the set
  • set.pop( )  - removes a random value Apna C
  • set.union( set2 )  - combines both set values & returns new
  • set.intersection( set2 )  - combines common values & returns new new 

 

Loops in Python-

 Loops are fundamental programming constructs that allow you to execute a block of code repeatedly until a certain condition is met.

Loops are used for instructions. Loops are used for sequential traversal. For traversing lists, strings, tuples, etc.

 

1. While loop


Used to execute a block of code as long as a certain condition is True.

 

Syntex -

 

While conditions: 

 

Positive Loop

i = 1

while i <= 10 :

i += 1

print(i)

 

Negative Loop

i = 10

while i >= 1 :

i -= 1

print(i)

 

Infinite Loop

i = 1

while i < 10:

i += 1

print(i)


i = 10

while i > 1:

i -= 1

print(i)

 

2 - For Loops

 Used to iterate over a sequence of items, like lists, tuples, strings, or dictionaries.

Syntax -


for el in list: 


el(element)

list- list=(1,2,3,4)


Example -


List example


1- list=(1, 2, 3, 4)

for el in list:

print(el)


Tuples example


tups ={ 1, 2, 3, 4 }

for el in tups:

print(el)


String example 


str= ("Gopal", "Shyam", "Radhe", "Krishna")

for el in str:

print(el)


Print Character-

 

Str="RAHUL"

for char in str:

print(char)

 

Indexing For loops-

num = [ 23, 43 ,23 , 54 ,75 ,35 ,34, 23]

x=23

idx= 0

for el in num:

if(el == x):

print("found", idx)

print(el)

idx += 1

else:

print("Number Ended")


Use of else -


The else statement is primarily used in two contexts in Python:


str= ("Gopal", "Shyam", "Radhe", "Krishna")

for el in str:

print(el)

else:

print(" String End")


3 -Use Range 

Range functions return a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number. range( start?, stop, step?)


Syntex

 

range(start?,stop,step?)

for el in range(10):

print(el)


for el in range(1, 6)

print(el)


for el in range(1, 6, 2)

print(el)


Use range odd number -

for el in range(1, 100, 2):

print(el)


Use range even number-

for el in range(2, 100, 2):

print(el)


Statement


Python, a statement is a complete instruction that the Python interpreter can understand and execute. It forms the building block of your Python programs.

 

1. Pass Statement - 

A statement pass is a null statement that does nothing. It is used as a placeholder for future code.

for el in range(10):

pass

print(el)


2 -Break Statement


The break statement in Python is used to prematurely exit a loop (either for or while) when a certain condition is met.


str= "rahul"

for el in str:

if(el == 'u'):

print(" found")

print(el)

print("Char Finding....")

else:

print("Char End")


Use break statement 


str= "rahul"

for el in str:

if(el == 'u'):

print(" U found")

break # (this use break statement)

print(el)

print(" U Finding....")

else:

print("Char End")


Continue Statement -


The continue statement in Python is used to control the flow within a loop (for or while). It instructs the loop to skip the remaining code for the current iteration and immediately jump to the beginning of the next iteration.

 

str= "Bharadwaj"

for el in str:

if(el == 'w'):

print(" W found")

continue  (this use break statement)

print(el)

print("W Finding....")

else:

print("Char End")






Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.