Featured

    Featured Posts

Install the software and set the path


Install the software and set the path :-

·         Download the software from internet based on your operating system.

·         The software is different from 32-bit operating and 64-bit operating system.

·         To download the software open the fallowing web site.

http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

For 32-bit operating system please click on 
Windows x86:- 32- bit operating system
For 64-bit operating system please click on
Windows x64 :- 64-bit operating system

JDK Folder Hierarchy



How to Set Path and Classpath in Java

Java Classpath Setting

Path Variable

·         Path variable is set for providing path for all java tools like java, javac,javap, javah, jar, appletviewer which are use in java programming. These all tools are available in bin folders so we set path upto bin folders.

Classpath Variable

·        Classpath variable is set for providing path for predefined java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

 

Why set path ?

·        The following programming error is general for all java programmers when they compile any java program.

·        'javac' is not recognized as an internal or external command, operable program or batch file.

·        When you get this type of error, then your operating system cannot find the java compiler (javac). To solve this error you need to set the PATH variable.

·        Javac is a tool which is available in bin folder so you must set the PATH upto bin folder. In a bin folder all tools are available like javap, javah, jar, javac, java, appletviewer etc. These all tools are used for different-different purpose.



set the path and classpath

·         Go on my computer icon and right click, after that click on properties option.



·         Now click on advance setting

·         Click on advance


·         Click on Advance variables



·         Click on new button which is below the first box.


·         Now one dialog box is appear, now ignore this but do not close.



·         Now open my computer open c:/ > Programs Files > java > java1.6.0 > bin copy this path




·         Now come back on previous open dialogbox and write variable name 'path' and for variable value paste all copied path up to bin folder. Put.;at the end. It (.) select all the tools from bin folder.


·         Now open my computer open c:/ > Programs Files > java > java1.6.0 >jre > lib > rt.jar copy this path

·         Note:rt.jar is available in lib folder this jar files contains all classes of jdk.


·         Now again come back on Environment variable dialogbox and click on new. Now one box is open and write path variable as 'classpath' and for variable value paste all copied path up to rt.jar. Put dot (.)At the end. It (.) select all the classes from lib folder.






·         Note: Finally after set classpath Restart your system, or you can re-open command prompt.


·         Difference between path and classpath in Java

Path

·         Path variable is set for provide path for all java tools like java, javac, javap, javah, jar, appletviewer which are use at the time of java programming like for run java any program we use java tool and for compile java code use javac tool. These all tools are available in bin folder so we set path upto bin folder.

classpath

·         classpath variable is set for provide path of all java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Difference between path and classPath

 

path

classpath

path variable is set for provide path for all java tools like java, javac, javap, javah, jar, appletviewer

classpath variable is set for provide path of all java classes which is used in our application.

 









  

Dictionary

 

Different Ways to Creating a dictionary in Python

ü    Dictionary consists of key-value pairs. It is enclosed by curly braces {}.


ü    A dictionary can be created by enclosing item, separated by commas, in curly braces {} Where item is nothing item is a key-value pair and the key and the value is separated by a colon (:). This pair is known as item. Items are separated from each other by a comma (,).

 

ü    keys must be of an immutable data type such as strings, numbers, or tuples.

 

§  We can create empty dictionary object as follows.

d={}

print(d)

print(type(d))

# The Output Will be

{}

<class 'dict'>

We can create dictionary object with some element If we know data in        

advance   as follows





d={"name":"john","age":20,"country":"India"}

print(d)

print(type(d))

# The Output Will be

{'name': 'john', 'age': 20, 'country': 'India'}

<class 'dict'>


§  We can create dictionary object with dynamic some element as follows.

d=eval(input("Enter dictionary :"))  

print(d) 

print(type(d)) 

 

# The Output Will be

Enter Dictionary:{101:'sachin',102:'Vijay',103:'Ajay'}

{101: 'sachin', 102: 'Vijay', 103: 'Ajay'}

<class 'dict'>

 

§  We can create dictionary object using built-in class: dict()  as follows.

 

§  The dict() constructor can be used to create dictionaries from keyword arguments, or from a single iterable of key-value pairs, or from a single dictionary and keyword arguments

d = dict()     

print(d) 

print(type(d)) 

# The Output Will be

{}

<class 'dict'>

 

d = dict(a=1, b=2, c=3)  

print(d)

Output:

{'a': 1, 'b': 2, 'c': 3}

d=dict([('a', 1), ('b', 2), ('c', 3)])  

print(d)

Output:

{'a': 1, 'b': 2, 'c': 3}

 

d=dict({'a' : 1, 'b' : 2}, c=3)

print(d)

Output:

{'a': 1, 'b': 2, 'c': 3}

 

§  We can create dictionary object using Python Dictionary fromKeys() inbuilt function as follows.

 

§  Python Dictionary fromKeys() is an inbuilt function that creates a new dictionary from the given sequence of elements .

dictionary.fromkeys(keys, value)

§  This Method takes two parameter first parameter is key that is required parameter.and it is an iterable specifying the keys of the new dictionary.

 

 

§  The Second parameter is value parameter that is optional and the value for all keys. The default value is None.

 

§  Python fromkeys() method returns a new dictionary with a given sequence of elements as the keys of the dictionary.

employees = ['Scott', 'Mark', 'John']

defaults = {'Application Developer', 1000}

 

resDict = dict.fromkeys(employees, defaults)

print(resDict)

Output:

{

'Scott': {1000, 'Application Developer'}, '

Mark': {1000, 'Application Developer'}, '

John': {1000, 'Application Developer'}



How to access Element from the dictionary

§  The elements of a dictionary can be accessed via a key.

d= {101: 'sachin', 102: 'Vijay', 103: 'Ajay'}

print(d[101])#The OutPut:sachin

print(d[102])#The OutPut:Vijay

print(d[103])#The OutPut:Ajay

    If the specified key is not available then we will get KeyError.

print(d[104])

KeyError: 104

 

How We can Avoid KeyError Exceptions

 

§  One common pitfall when using dictionaries is to access a non-existent key. This typically results in a KeyError exception.

 

§  One way to avoid key errors is to use the dict.get method, which allows you to specify a default value to return in the case of an absent key.

                    value = mydict.get(key, default_value)

§  Which returns mydict[key] if it exists, but otherwise returns default_value. Note that this doesn't add key to mydict.

 

d={101: 'sachin', 102: 'Vijay', 103: 'Ajay'}

print(d[101])#The OutPut:sachin

print(d[102])#The OutPut:Vijay

print(d[103])#The OutPut:Ajay

print(d.get(104,'Hello'))#The OutPut:Hello

 

§  In the given example 104 key is not exist if we try to fetch element by using 104 key then we get default value hello because 104 is not key of this given dict. this doesn't add key to dict.

 

§  if you want to retain that key value pair, you should use mydict.setdefault(key, default_value), which does store the key value pair.

 

d={101: 'sachin', 102: 'Vijay', 103: 'Ajay'}

print(d[101])#The OutPut:sachin

print(d[102])#The OutPut:Vijay

print(d[103])#The OutPut:Ajay

print(d.setdefault(104,'Hello'))#The OutPut:Hello

print(d)

Output:

sachin

Vijay

Ajay

Hello

{101: 'sachin', 102: 'Vijay', 103: 'Ajay', 104: 'Hello'}

 

 

§  An alternative way to deal with the problem.

§  You could also check if the key is in the dictionary.

if key in mydict:

    value = mydict[key]

else:

    value = default_value

 

 

 

 

How to Check that given key or given value exists in a dictionary or not

# Create first dictionary

d1={'a':1,'b':2,'c':3}

#Check given key exists in a dictionary

print('a' in d1)#The Output:True

#Another Way Check given key exists in a dictionary

print('a' in d1.keys())#The Output:True

#Check given Value exists in a dictionary

print(2 in d1.values())#The Output:True

 

 

Iterating over a Dictionary

§  You can iterate over the dictionary elements using for loop like below:

for element in dictionary:

       do operation

d = {'a': 1, 'b': 2, 'c':3}

for key in d:

print(key, d[key])

Output:

a 1

b 2

         c 3

 
















www.CodeNirvana.in

Powered by Blogger.

About

Site Links

Popular Posts

Translate

Total Pageviews