Python – Coding with fun

How to reverse a string?

s = 'abcdef'

s = s[::-1]

print s 

Explanation: “::” is known as stride notation

s[::2]
s[::-2]

Returns “ace” and “fdb” respectively.

How to read from console in Python?

name = raw_input()

Want to read an integer?

num = raw_input()
num = int(num) # raw_input returns string, so convert it to integer

Swap values in python:

a, b = b, a

My motivation to learn python is: life is short – you need Python

How to create non-editable JTable:

Override the method “isCellEditable” and implement as needed, such as:

//instance table model
DefaultTableModel tableModel = new DefaultTableModel(){
   @Override
   public boolean isCellEditable(int row, int column) {
      return false;
   }
};
//...others
table.setModel(tableModel);

Generics are not supported in -source 1.3 error in Maven Project

When compiling via maven project, you may have encountered following error:

generics are not supported in -source 1.3 error in Maven Project. use -source 5 or higher to enable generics.

Annotation is not allowed in -source 1.3 use -source 5 or higher to enable annotation.

To solve this you must add the below lines at your pom.xml :

<build>
 <plugins>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <configuration>
    <source>1.5</source>
    <target>1.5</target>
   </configuration>
  </plugin>
 </plugins>
</build>