Blog

- Showing all posts in category: Python
# Neat tricks with Python arrays
Published on 24-10-2019

As a Java developer Python amazes me how things can be done stupid simple. 

Try to reverse a string in java:

    public static void main(String[] args) {
        String mytext = "abcdefg";
        StringBuilder result = new StringBuilder();
        for (int i = mytext.length(); i > 0; i--) {
            result.append(mytext.charAt(i-1));
        }
        System.out.println(result);
    }

 

Do it in Python:

mytext = "abcdefg"[::-1]
print(mytext)

 

The array indexing and slicing explained:

"abcdefg"[<start>:<stop>:<steps>]

 

  Value Explaination
"abcdefg"[5] "f" Get the element on the 5th index number
"abcdefg"[2:] "cdefg" Slice the array from index 2 to the end
"abcdefg"[:2] "ab" Slice the array to index 2(exclusive)
"abcdefg"[1:4] "bcd" Get all elements starting from index 1 to index 4(exclusive)
"abcdefg"[2:7:2] "ceg" Get all elements starting from index 1 to index 7(exclusive), with steps of 2
"abcdefg"[::2] "aceg" From the beginning to the end, take steps of 2
"abcdefg"[::-1] "gfedcba" Take steps of -1 which actually iterates in reverse order.

 

# Python collections
Published on 25-06-2019

Like every programming language Python basic collections exists of the following types:

List:
Storing unordered a sequence of multiple items of data like strings, integers etc.

#Initialization
my_list = [123, 'abc', 'hello']

#Add an item
my_list.append('world')

#Print the list contents
print(my_list)
#[123, 'abc', 'hello', 'world']

#Print the second one
print(my_list[1])
#abc

#Reassign the second
my_list[1] = 'Foo'

#Print the list contents
print(my_list)
#[123, 'Foo', 'hello', 'world']

#Print the size
print(len(mylist))
#4

#Get the index of an item
mylist.index('hello')
#2

 

Dictionary
Storing an unordered sequence of key value pairs

#Initialization
my_dict = {'a': 344, 34:'Foo', 'b': 'Bar'}

#Get the entry with key 'b'
my_dict.get('b')
#Bar

#Add or update the entry with key 'b'
my_dict.update({'b': 123})

#Print contents
print(my_dict)
#{'a': 344, 34: 'Foo', 'b': 123}

 

Tuple
An immutable unordered sequence of items

#Initialization
my_tuple = (1, 222, 1, 'hello', 'world', 'hello', 'hello')

#Get the number of occurence of item 'hello'
my_tuple.count('hello')
#3

#Get the index of the first occurence of 'hello'
my_tuple.index('hello')
#3

 

Set
An immutable unordered unique sequence of items

#Initialization
my_set = set()

#Add items
my_set.add(1)
my_set.add(222)
my_set.add('Hello')
my_set.add('Hello')
my_set.add('Hello')

#Print contents
print(my_set)
#{1, 222, 'Hello'}

#Add a sequence
my_set.update([5, 'Wow'])

#Print contents
print(my_set)
#{1, 222, 5, 'Hello', 'Wow'}

#Remove item
my_set.remove('Hello')

#Print contents
print(my_set)
#{1, 222, 5, 'Wow'}

 

# Python mutability
Published on 25-06-2019

Mutability of built-in Pyton data types

Type Description Immutable
bool Boolean value Yes
int integer Yes
float floating point number Yes
list mutable sequence of objects No
tuple immutable sequence of objects Yes
str Character string Yes
set unordered set of distinct objects No
frozenset immutable variant of set Yes
dict associative key value mapping No