iTestee Coding,Python Python Data handling

Python Data handling

#A list:

#Preserves insertion order.
#Can contain different data types.
#Is mutable, meaning you can change, add, or remove items.

raw = ["  pandas ", "FLASK  ", " SQL", "  Airflow "] # List
mixed = [10, "Python", 3.14, True]

Basic selections:

print(raw)           #--> ['  pandas ', 'FLASK  ', ' SQL', '  Airflow ']
print(raw[0])        #-->   pandas 
print(raw[0].strip())#--> pandas
print(raw[0:2])      #--> ['  pandas ', 'FLASK  ']
print(raw[1:])       #--> ['FLASK  ', ' SQL', '  Airflow ']
print(raw[:3])       #--> ['  pandas ', 'FLASK  ', ' SQL']
print(raw[:])        #--> ['  pandas ', 'FLASK  ', ' SQL', '  Airflow ']

# Loop
print('# Loop with index')
for item in raw:
    print(item)

#-->
#  pandas 
#FLASK  
# SQL
#  Airflow 

print('# Loop with index')
for i in range(len(raw)):
    print(i, raw[i])

#0   pandas 
#1 FLASK  
#2  SQL
#3   Airflow 


print('## Loop with index using enumerate')
for index, value in enumerate(raw):
    print(index, value.strip())  

#0 pandas
#1 FLASK
#2 SQL
#3 Airflow
# Check existent

print(" SQL" in raw)
#True
s = "abcdefghijklmno"

| Positive Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| -------------- | - | - | - | - | - | - | - | - | - | - | -- | -- | -- | -- | -- |
| Character      | a | b | c | d | e | f | g | h | i | j | k  | l  | m  | n  | o  |

| Negative Index |-15|-14|-13|-12|-11|-10|-9 |-8 |-7 |-6 |-5 |-4 |-3 |-2 |-1 |
| -------------- | --| --| --| --| --| --| --| --| --| --| --| --| --| --| --|
| Character      | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o |

start → Starting index (inclusive). Defaults to 0 if omitted.
stop  → Stopping index (exclusive). Defaults to the end of the string if omitted.
step  → how many positions to move (default = 1)

print(s[-4:]) 
print(s[:-3])
print(s[-5:-2])
print(s[-8:-1:2])

t = (1, 2, 3) #Tuple
print(t) #--> (1, 2, 3)
print(t[0]) #--> 1
print(t[-1]) #--> 3
print(t[0:2]) #--> (1, 2)
print(t[:]) #--> (1, 2, 3)

for number in t:
    print(number)
#-->
#1
#2
#3

for index, value in enumerate(t):
    print(index, value) 
#-->
#0 1
#1 2
#2 3

print(2 in t) #--> True     

a, b, c = t

print(a) #--> 1
print(b) #--> 2
print(c) #--> 3

ls1 = list(t);

print(ls1) #--> [1, 2, 3]
s = {1, 2, 3} #SET
print(s) #--> {1, 2, 3}

for value in s:
    print(value)

print(10 in s)
print(1 in s)

lst = list(s);
print(lst[1])
engineer = {
    "name": "Satsara",
    "age": 30,
    "city": "Auckland",
    "salary": 95000,
    "experience": 3
} # Disct
print(engineer) #--> 
#{'name': 'Satsara', 'age': 30, 'city': 'Auckland', 'salary': 95000, 'experience': 3}

print(engineer["name"]) #--> Satsara

print(engineer.get("city")) #--> Auckland

print(list(engineer.items())[1]) #--> ('age', 30)

for  val in engineer.items():
    print(val[0])
    print(val[1])
#-->
#name
#Satsara
#age
#30
#city
#Auckland
#salary
#95000
#experience
#3


for val2 in engineer.values():
    print(val2)

#Satsara
#30
#Auckland
#95000
#3

for val3 in engineer.keys():
    print(val3)  

#name
#age
#city
#salary
#experience  

for key, value in engineer.items():
    print(key, value)  

#name Satsara
#age 30
#city Auckland
#salary 95000
#experience 3  

for ind, va3 in enumerate(engineer.items()):
    print(str(ind) + ',' + str(va3))

#0,('name', 'Satsara')
#1,('age', 30)
#2,('city', 'Auckland')
#3,('salary', 95000)
#4,('experience', 3)

for ind, va3 in enumerate(engineer.items()):
    print(str(ind) + ',' + str(va3[0]))
    print(str(ind) + ',' + str(va3[1]))

#0,name
#0,Satsara
#1,age
#1,30
#2,city
#2,Auckland
#3,salary
#3,95000
#4,experience
#4,3
employee = {
    "name": "Satsara",
    "skills": ["Python", "SQL", "`Pandas"],
    "experience": (1,2,4),
    "cities": {'name': 'Auckland', 'country': 'New Zealand', 'population': 1.6}
 }

# Complex

print(employee.get("name")) #--> Satsara
print(employee.get('skills')[:2]) #--> ["Python", "SQL"]
print(list(employee.get('experience'))[0:3]) --> [1,2,4]

print(employee.get('cities').get('country')) #--> New Zealand

for key, values in employee.get('cities').items():
    print(key, values)

#name Auckland
#country New Zealand
#population 1.6

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + 3 =
Powered by MathCaptcha

Related Post