Python-style extensions for Kotlin - Write Kotlin code with Python's elegant syntax!
PyKot brings Python's beloved syntax and built-in functions to Kotlin, making your code more concise and expressive. No more verbose Java-style operations - embrace Python's elegance in your Kotlin projects!
- String Operations: Python-style string multiplication, formatting, and methods
- Collection Magic: List multiplication, negative indexing, and Python-like methods
- Functional Programming: Built-in
map
,filter
,reduce
,zip
functions - Utility Functions:
len()
,print()
,type()
,isinstance()
, and more - Numeric Extensions:
pow
,abs
,min
,max
,sum
,divmod
- Dictionary Operations: Python-style dict methods and membership testing
- Range Functions:
range()
andxrange()
just like Python
Add it to your build.gradle
:
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.xcodebn:pykot:1.0.0'
}
import com.xcodebn.pykot.*
fun main() {
// String multiplication (Python style!)
println("=" * 50) // Prints 50 equals signs
// List multiplication and operations
val numbers = listOf(1, 2, 3) * 2 // [1, 2, 3, 1, 2, 3]
println("Length: ${len(numbers)}") // Length: 6
// Negative indexing
println("Last item: ${numbers.at(-1)}") // Last item: 3
// Python-style range
for (i in range(5)) { // 0, 1, 2, 3, 4
print(i, end=" ")
}
// String formatting
val message = "Hello {0}, you have {1} messages!".format("Alice", 5)
println(message) // Hello Alice, you have 5 messages!
// Dictionary operations
val myDict = dict("name" to "PyKot", "version" to "1.0.0")
println("name" `in` myDict) // true
// Functional programming
val doubled = map(listOf(1, 2, 3)) { it * 2 } // [2, 4, 6]
val evens = filter(listOf(1, 2, 3, 4)) { it % 2 == 0 } // [2, 4]
}
// String multiplication
"Hi! " * 3 // "Hi! Hi! Hi! "
// String formatting
"User: {0}, Age: {1}".format("John", 25) // "User: John, Age: 25"
// Membership testing
"world" `in` "hello world" // true
// Python methods
" hello ".strip() // "hello"
"hello".upper() // "HELLO"
"hello".startswith("he") // true
"hi".center(6, "*") // "**hi**"
// Smart splitting
"a b c".split() // ["a", "b", "c"]
// Joining
listOf("a", "b", "c").join("-") // "a-b-c"
// List multiplication
listOf(1, 2) * 3 // [1, 2, 1, 2, 1, 2]
// Negative indexing (Python style!)
val list = listOf("a", "b", "c", "d")
list.at(-1) // "d" (last element)
list.at(-2) // "c" (second to last)
// List slicing
list[1..2] // ["b", "c"]
// Python-style methods
val mutableList = list("a", "b", "c")
mutableList.append("d")
mutableList.extend(listOf("e", "f"))
val lastItem = mutableList.pop() // removes and returns last
// Enumerate like Python
listOf("apple", "banana").enumerate() // [(0, "apple"), (1, "banana")]
// List comprehensions (sort of)
listOf(1, 2, 3, 4).listcomp { it * 2 } // [2, 4, 6, 8]
listOf(1, 2, 3, 4).listcomp({ it % 2 == 0 }) { it * 2 } // [4, 8]
// Create dictionaries
val myDict = dict("name" to "PyKot", "type" to "Library")
// Python-style methods
myDict.get("name", "Unknown") // Returns value or default
myDict.setdefault("version", "1.0") // Sets if not exists
myDict.update(dict("author" to "xcodebn"))
// Membership testing
"name" `in` myDict // true
// Dictionary properties (Python style)
myDict.keys // Set of keys
myDict.values // Collection of values
myDict.items // Set of entries
// Basic ranges
range(5) // 0, 1, 2, 3, 4
range(2, 8) // 2, 3, 4, 5, 6, 7
range(1, 10, 2) // [1, 3, 5, 7, 9]
// Lazy ranges (memory efficient)
xrange(1000000) // Creates lazy sequence, not full list
// Convert to list when needed
range(5).toList() // [0, 1, 2, 3, 4]
// Length function for everything
len("hello") // 5
len(listOf(1, 2, 3)) // 3
len(mapOf("a" to 1)) // 1
// Type checking
type("hello") // "String"
isinstance<String>("hi") // true
// Conversions
str(123) // "123"
int("456") // 456
float("3.14") // 3.14
bool("") // false
bool("hello") // true
// Object inspection
id(myObject) // Hash code
dir(myObject) // List of methods
// Enhanced print
print("Hello", "World", sep="-", end="!\n") // "Hello-World!"
// Power operator
2 pow 3 // 8
2.5 pow 2.0 // 6.25
// Math functions
abs(-42) // 42
min(5, 2, 8, 1) // 1
max(5, 2, 8, 1) // 8
round(3.14159, 2) // 3.14
// Aggregation
sum(listOf(1, 2, 3, 4)) // 10
// Division with remainder
divmod(7, 3) // Pair(2, 1) β 7 = 2*3 + 1
// Constants
PI // 3.141592653589793
E // 2.718281828459045
// Top-level functions (Python style)
map(listOf(1, 2, 3)) { it * 2 } // [2, 4, 6]
filter(listOf(1, 2, 3, 4)) { it % 2 == 0 } // [2, 4]
reduce(listOf(1, 2, 3, 4)) { a, b -> a + b } // 10
// Zip operations
zip(listOf(1, 2), listOf("a", "b")) // [(1, "a"), (2, "b")]
zip(listOf(1, 2), listOf("a", "b"), listOf(true, false)) // Triple
// Boolean aggregation
any(listOf(true, false, false)) // true
all(listOf(true, true, false)) // false
// Iterator operations
val iter = iter(listOf(1, 2, 3))
next(iter) // 1
next(iter, -1) // 2 or -1 if no more
no reason i was just bored :')
Before PyKot:
val line = "=".repeat(50)
val doubled = mutableListOf<Int>()
repeat(3) { doubled.addAll(listOf(1, 2, 3)) }
val lastItem = myList[myList.size - 1]
val message = "Hello $name, you have $count messages!"
With PyKot:
val line = "=" * 50
val doubled = listOf(1, 2, 3) * 3
val lastItem = myList.at(-1)
val message = "Hello {0}, you have {1} messages!".format(name, count)
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Commit your changes:
git commit -m 'Add amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
- More Python built-in functions
- Additional string methods
- Set operations
- File I/O utilities
- More comprehensive documentation
- Performance optimizations
This project is licensed under the MIT License - see the LICENSE file for details.
If PyKot makes your Kotlin code more Pythonic and enjoyable, please give it a βοΈ!
xcodebn - @xcodebn
Project Link: https://github.com/xcodebn/pykot