PySpark, BigData, SQL, Hive, AWS, Python, Unix/Linux, Shortcuts, Examples, Scripts, Perl
Jan 2, 2013
Dict keys and values in Python
Dict in Python consists of Key & Value pairs. This is similar to Hash concept in Perl language.
Keys are unique in Dict, Values are not necessarily unique in nature.
relatives = {"Lisa" : "daughter", "Bart" : "son", "Marge" : "mother", "Homer" : "father", "Santa" : "dog"}
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
for member in sorted(relatives.keys()):
#print "\n",member
print("\n",member)
for member in sorted(relatives.values()):
#print "\n",member
print("\n",member)
Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Dict Example in Python
Today, we discuss about the Dict concept in Python.
Dict in Python consists of Key & Value pairs. This is similar to Hash concept in Perl language.
Keys are unique in Dict, Values are not necessarily unique in nature.
Let's discuss with an example, let's dive & have fun :
relatives = {"Lisa" : "daughter", "Bart" : "son", "Marge" : "mother", "Homer" : "father", "Santa" : "dog"}
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
#Keys are unique
relatives['Marge'] = "mother";
relatives['marge'] = "mother1";
#'Marge' and 'marge' are different keys
for member in sorted(relatives.keys()):
print("\n",member, "=>", relatives[member])
#print "\n",member
for member in sorted(relatives.values()):
print("\n",member)
#print "\n",member
for key, value in relatives.items():
print("\n", key, "=>", value)
#print "\n", key, "=>", value
Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Dict in Python consists of Key & Value pairs. This is similar to Hash concept in Perl language.
Keys are unique in Dict, Values are not necessarily unique in nature.
Let's discuss with an example, let's dive & have fun :
relatives = {"Lisa" : "daughter", "Bart" : "son", "Marge" : "mother", "Homer" : "father", "Santa" : "dog"}
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
#Keys are unique
relatives['Marge'] = "mother";
relatives['marge'] = "mother1";
#'Marge' and 'marge' are different keys
for member in sorted(relatives.keys()):
print("\n",member, "=>", relatives[member])
#print "\n",member
for member in sorted(relatives.values()):
print("\n",member)
#print "\n",member
for key, value in relatives.items():
print("\n", key, "=>", value)
#print "\n", key, "=>", value
Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Append to list in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
new_insp_ppl = "prabhath"
inspiring_ppl.append(new_insp_ppl)
o/p:
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "prabhath"]
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Delete the last name from the list in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "Abdul Kalam"]
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
del inspiring_ppl[-1] #removes Abdul Kalam
print ("The following inspiring_ppl are :", inspiring_ppl)
#print "The following inspiring_ppl are :", inspiring_ppl
#o/p:
#inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Remove an element from List in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
new_inspiring_ppl = inspiring_ppl[:]
#Note:
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
for person in new_inspiring_ppl:
print("Do you want to keep person", person, "?")
#print "Do you want to keep person", person, "?"
answer = input("yes/no ")
#answer = raw_input ("yes/no ")
if answer != "yes":
inspiring_ppl.remove(person)
print(inspiring_ppl)
#print inspiring_ppl
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Python difference between list copy and reference
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
#The following Shares same memory location
inspiring_ppl_1 = inspiring_ppl
inspiring_ppl.append("Abdul Kalam")
inspiring_ppl_1.append("Abraham Lincoln")
#Therefore inspiring_ppl, inspiring_ppl_1 contains the same contents including "Abdul Kalam" and "Abraham Lincoln"
#Python does not have variables like C
#In Python variables are just tags attached to objects
#The following shares different memory location
inspiring_ppl_2 = inspiring_ppl[:]
inspiring_ppl.append("Swami Vivekananda")
print(inspiring_ppl)
o/p: ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "Abdul Kalam", "Abraham Lincoln"]
print(inspiring_ppl_1)
o/p: ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "Abdul Kalam", "Abraham Lincoln"]
print(inspiring_ppl_2)
o/p: ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "Swami Vivekananda"]
Check an element exists in an array/list in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
another_person = input("Enter person Name : ")
if another_person in inspiring_ppl:
inspiring_ppl.remove(another_person)
else:
inspiring_ppl.append(another_person)
print(inspiring_ppl)
How to copy a list into another
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
more_inspiring_ppl = inspiring_ppl[:]
more_inspiring_ppl.reverse()
print inspiring_ppl #2.X Version
#print (inspiring_ppl) #3.X Version
print more_inspiring_ppl #2.X Version
#print (more_inspiring_ppl) #3.X Version
For Loop in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
for person in inspiring_ppl:
print "Hello", person + ", how are you?" #3.X Version
#print("Hello", person + ", how are you?") #2.X Version
Reading a File with line numbers
file = open("read.txt","r")
text = file.readlines()
file.close()
counter = 1
for line in text:
print counter, line,
counter = counter +1
File Operations in Python
file = open("read.txt","r")
text = file.readlines() #Read lines from read.txt
file.close()
file2 = open ("write.txt", "w")
#Write all lines from read.txt into write.txt
#It overrides if any content already present in write.txt
file2.writelines(text)
file2.close
file2 = open ("append.txt", "a")
#It appends content from read.txt into append.txt
file2.writelines(text)
file2.close
Print File Content in Python
file = open("read.txt","r")
text = file.readlines() #Reads the file content
file.close()
for line in text:
print line #Python 2.X Versions
#print(line) #Python 3.X Versions
Print File in Reverse Order in Python
file = open("read.txt","r")
text = file.readlines() #Reads the file content
file.close()
text.reverse() #It reverses the content
for line in text:
print line #Python 2.X Versions
#print(line) #Python 3.X Versions
Labels:
python,
python_basics,
python_file,
python_scripts
Subscribe to:
Posts (Atom)