Kotlin: 100 Simple Codes


beginner-friendly collection of easy-to-understand Kotlin examples.


Each code snippet is designed to help you learn programming concepts step by step, from basic syntax to simple projects. Perfect for students, self-learners, and anyone who wants to practice Kotlin in a fun and practical way.


Codes:

1. Hello World

2. Variables and Constants

3. If-Else Statement

4. When Statement (Switch)

5. For Loop

6. While Loop

7. Functions

8. Return Value from Function

9. Array Example

10. List Example

===

11. Mutable List

12. Map Example

13. Mutable Map

14. Class Example

15. Constructor with Default Value

16. Nullable Variable

17. Safe Call Operator

18. Elvis Operator

19. Data Class

20. Loop with Index

===

21. Lambda Function

22. Higher-Order Function

23. Filter a List

24. Map a List

25. String Interpolation

26. String Templates with Expressions

27. Read-Only vs Mutable List

28. Check Element in List

29. Exception Handling

30. Null Check with let

===

31. For Loop with Step

32. For Loop in Reverse

33. Break in Loop

34. Continue in Loop

35. Check String Empty or Not

36. Compare Two Numbers

37. Array Access by Index

38. Loop Through Map

39. Default Parameters in Function

40. Named Arguments

===

41. Range Check

42. Function Returning Unit

43. Multiple Return Statements

44. Chained Method Calls

45. Function Inside Function

46. Function Expression Syntax

47. Array Size

48. String to Int Conversion

49. Safe String to Int Conversion

50. Repeat Block

===

51. Sealed Class

52. Object Expression (Anonymous Object)

53. Singleton using Object Keyword

54. Extension Function

55. Enum Class

56. Use Enum in When Statement

57. Type Alias

58. Destructuring Declarations

59. Companion Object

60. Simple Interface Implementation

===

61. Abstract Class

62. Lateinit Variable

63. Initialization Block

64. Secondary Constructor

65. Nested Class

66. Inner Class

67. Generic Function

68. Generic Class

69. Custom Getter

70. Custom Setter

===

71. String Equality

72. Loop with Range Until

73. Using Pair

74. Triple Example

75. Check Type with is

76. Smart Cast

77. Type Casting with as

78. Safe Casting with as?

79. Loop Through Characters of String

80. Sum of List

===

81. Min and Max of List

82. Sort List

83. Reverse List

84. Count Items in List

85. All / Any Conditions

86. Check if List is Empty

87. Join List to String

88. Take and Drop

89. Zipping Lists

90. Unzipping Pairs

===

91. Chunked List

92. Windowed List

93. Flatten List

94. FlatMap

95. Remove Duplicates

96. Group By

97. Associate By

98. Measure Execution Time

99. Repeat with Index

100. Create Range and Convert to List

===