Author Archive

Dictionary in python with example

A dictionary is mutable and is another container type that can store any number of Python objects, including other container types. Dictionaries consist of pairs (called items) of keys and their corresponding values.

Python dictionaries are also known as associative arrays or hash tables.

dict = {‘Name’: ‘John’, ‘Age’: ’20′, ‘Gender’: ‘Male’}
print “dict['Name']: “, dict['Name'];
print “dict['Age']: “, dict['Age'];
print “dict['Gender']: “, dict['Gender'];

 

Python tuple with example

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can’t be changed i.e., tuples are immutable and tuples use parentheses and lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values and optionally you can put these comma-separated values between parentheses also. For example:

tup1 = (‘United States’, ‘John’, 2014);
tup2 = (1, 2, 3, 4, 5 );
tup3 = “a”, “b”, “c”, “d”;
print “tup1[0]: “, tup1[0]
print “tup2[1:2]: “, tup2[1:2]

Python list example

The list is a most flexible datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. The items in a list can be of different data type.

Examples:

list1 = ['United States', 'John', 2014];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

 

print “list1[0]: “, list1[0]
print “list2[1:2]: “, list2[1:2]

The following example will show you how to create an integer array and print it in ascending and descending order.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print numbers[0:5] #print numbers from 1 to 5
print numbers #print all numbers
print numbers[0:10:2] #odd nums
print numbers[1:10:2] #even numbers
print numbers[10::-1] #numbers in descending order

//output:

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 3, 5, 7, 9]
[2, 4, 6, 8, 10]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

String array example in python

The following example will show you how to use string array in python:

#String array example
fruits = ["banana", "apple",  "mango", "orange", "grapes", "strawberries"];
print ‘Fruit 1:’, fruits[0];
print ‘Fruit 2:’, fruits[1];
print ‘Fruit 3:’, fruits[2];
print ‘Fruit 4:’, fruits[3];
print ‘Fruit 5:’, fruits[4];
print ‘Fruit 6:’, fruits[5];

Android logging system(Log)

The Android logging system provides a mechanism for collecting and viewing system debug output. Logcat dumps a log of system messages, which include things such as stack traces when the emulator throws an error and messages that you have written from your application by using the Log class.
Log is a logging class that you can utilize in your code to print out messages to the LogCat. Common logging methods include:

v(String, String) (verbose) //use Log.v()
d(String, String) (debug) //use Log.d()
i(String, String) (information) //use Log.i()
w(String, String) (warning) //use Log.w()
e(String, String) (error) //use Log.e()

For Example:
Log.i(“MyTag”, “Item number ” + itemNumber);

Getting values from String.xml android

You can access a string.xml values in any of the following way:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="message">Welcome to Android</string>

</resources>

In the layout file, you can use it like following:

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/message" />

In the java file, you can access it like following:

String msg = context.getResources().getString(R.string.message);

Send an HTML email using Java

An example to send an HTML email from your machine, assumed that your localhost is connected to the internet and capable enough to send an email.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendHTMLEmail
{
public static void main ( String [] args )
{

// Recipient's email ID needs to be mentioned.
String to = "Recipient_email_id";

String from = "Sender_email_id";

String host = "localhost";

Properties properties = System.getProperties();

// Setup SMTP mail server
properties.setProperty ( "mail.smtp.host", host );

// Get the default Session object.
Session session = Session.getDefaultInstance ( properties );

try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage ( session );

// Set From: header field of the header.
message.setFrom ( new InternetAddress ( from ) );

// Set To: header field of the header.
message.addRecipient ( Message.RecipientType.TO,
new InternetAddress(to) );

// Set Subject: header field
message.setSubject ( "This is the Subject Line!" );

// Send the actual HTML message, as big as you like
message.setContent ( "<h1>This is actual message</h1>",
"text/html"  );

// Send message
Transport.send ( message );
System.out.println ( "Sent message successfully...." );
} catch ( MessagingException mex )  {
mex.printStackTrace();
}
}
}

Parsing Strings into Date in Java

The SimpleDateFormat class has a method called parse(), which tries to parse a string according to the format stored in the given SimpleDateFormat object. For example:

import java.util.*;
import java.text.*;

public class DateDemo {

public static void main(String args[]) {
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");

String input = "2014-02-18" ;
System.out.print(input + " Parses as ");
Date t;
try {
t = ft.parse(input);
System.out.println(t);
} catch (ParseException e) {
System.out.println("Unparseable using " + ft);
}
}
}

A Java program to send an email

An example to send a simple e-mail from your machine. Here it is assumed that your localhost (XAMPP)  is connected to the internet and capable enough to send an email.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail
{
   public static void main(String [] args)
   {    

      String to = "recipient_email_id";
      String from = "sender_email_id";    
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();
      // Setup SMTP mail server
      properties.setProperty("mail.smtp.host", host);
      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);
      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);
         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));
         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
         // Set Subject: header field
         message.setSubject("This is the Subject Line!");
         // Now set the actual message
         message.setText("Kindly consider this email as a test from SCHOGINI SYSTEMS");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException me) {
         me.printStackTrace();
      }
   }
}
Page 2 of 5712345...102030...Last »
We Can Build Your App for You. Lets Get Started !