Python and Kotlin: A comparison of the language basics

In this table I will compare some basic programming syntax and conventions between the Python and Kotlin programming languages.

Programming elementPythonKotlin
Commenting code:
# This is a comment// This is a comment
Multi-line comments:
""" This is a comment.
This is a comment, too.
This is a comment, too. """
/* This is a comment.
This is a comment, too.
This is a comment, too. */
Identifying variable type:
type(variable)In Kotlin there is no equivalent. if and is need to be used to check a variable type.

For example:
if (obj is String)
Creating a variable that contains an int :age = 25(val is immutable)
val age = 25
or explicit:
val age: Int = 25

(var is an updateable variable)
var negative_number = -25
or explicit:
var negative_number: Int = -25
Creating a variable that contains a float:probability = 0.62var probability = 0.62
or explicit:
var probability: Float = 0.62f
(notice the appended ‘f’)
Creating a variable that contains a string:
Python supports single, double, and triple quotes.
state = 'North Dakota'
or
state = "North Dakota"
or
state = """North Dakota"""
Double quotes only:
var state = "North Dakota"
or explicit:
var state: String = "North Dakota"
Denoting string literals:
Python uses an ‘r’ in front of the string.
path = r'\path\to\some\files'
or
path = r"\path\to\some\files"
or
path = r"""\path\to\some\files"""
Double backslash to escape:
var path = "\\path\\to\\some\\files"
String Concatenation:greeting = "Hello" + " world"  
or
greeting = str1 + str2
Example 1 using .plus():
val str1 = "Hello"
val str2 = "world"
val greeting = str1.plus(" ").plus(str2)

Example 2 using strings:
val greeting = "Hello" + " " + "world"

Example 3 using string variables:
val greeting = str1 + " " + str2

Example 4 using template expressions:
val greeting = "$str1 $str2"
Creating a list or array:fruit_list = ['apple', 'banana', 'cherry']Example 1 using mutableListOf():
var fruit_list = mutableListOf("apple", "banana", "cherry")

Example 2 using listOf():
var fruit_list = listOf("apple", "banana", "cherry")

Example 3 using explicit type:
var fruit_list = listOf<String>("apple", "banana", "cherry")

Example 4 mutable arrayListOf():
var fruit_list = arrayListOf("apple", "banana", "cherry")
Initializing an empty list or array:empty_list = []val empty_list = listOf<Int>()

val empty_list = arrayListOf<Double>()

val empty_list = mutableListOf<String>()

val empty_list: List<Int> = emptyList()
Getting the length of a list, array, dictionary, or map:length = len(fruit_list)val length = fruit_list.size
Python dictionary vs. Kotlin map:
fruit = {'a':'apple', 'b':'banana', 'c':'cherry'}val fruit = mapOf("b" to "banana", "a" to "apple", "c" to "cherry")
// {b=banana, a=apple, c=cherry}

val fruit = hashMapOf("b" to "banana", "a" to "apple", "c" to "cherry")
// {a=apple, b=banana, c=cherry}

val fruit = linkedMapOf("b" to "banana", "a" to "apple", "c" to "cherry")
// {b=banana, a=apple, c=cherry}

val fruit = sortedMapOf("b" to "banana", "a" to "apple", "c" to "cherry")
// {a=apple, b=banana, c=cherry}

val fruit = mutableMapOf("b" to "banana", "a" to "apple", "c" to "cherry")
// {b=banana, a=apple, c=cherry}
Initializing an empty dictionary or map:fruit = {}fruit = mapOf()

fruit = mutableMapOf<String, String>()
Getting keys from a dictionary or map:fruit_keys = fruit.keys()val fruit_keys = fruit.keys
// [b, a, c]
Getting values from a dictionary or map:fruit_values = fruit.values()val fruit_values = fruit.values
// [banana, apple, cherry]
Adding key-value pairs to a dictionary or map:fruit['o'] = 'orange'fruit["o"] = "orange"
or
fruit.put("o", "orange")
or
fruit += ("o" to "orange")
Deleting key-value pairs from a dictionary or object:del fruit['c']Deleting by key:
fruit.remove("b")
or
fruit.keys.remove("o")
or
fruit -= "a"

Deleting by value:
fruit.values.remove("grape")

Delete by both key and value:
fruit.remove("g", "grape")

Python was invented in the late 1980s (1989) by Guido van Rossum. Ideas for Python were adopted from Modula-3 and Lisp.

Kotlin was invented in 2011 by JetBrains. According to Tutorialspoint.com:

Kotlin is a new open source programming language like Java, JavaScript, etc. It is a high level strongly statically typed language that combines functional and technical part in a same place. Currently, Kotlin targets Java and JavaScript. It runs on JVM.

Kotlin is influenced by other programming languages such as Java, Scala, Groovy, Gosu, etc. The syntax of Kotlin may not be exactly similar to JAVA, however, internally Kotlin is reliant on the existing Java Class library to produce wonderful results for the programmers. Kotlin provides interoperability, code safety, and clarity to the developers around the world.