0% found this document useful (0 votes)
2 views1 page

Simple Python Code Examples

The document provides simple Python code snippets for various programming tasks including calculating the GCD of two numbers, performing exponentiation, finding the maximum of a list, identifying the most frequent words in a text file, and implementing linear, insertion, selection, and merge sort algorithms. Additionally, it includes a basic example of matrix multiplication. Each code snippet is designed to be straightforward and easy to understand.

Uploaded by

hecheskali099
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Simple Python Code Examples

The document provides simple Python code snippets for various programming tasks including calculating the GCD of two numbers, performing exponentiation, finding the maximum of a list, identifying the most frequent words in a text file, and implementing linear, insertion, selection, and merge sort algorithms. Additionally, it includes a basic example of matrix multiplication. Each code snippet is designed to be straightforward and easy to understand.

Uploaded by

hecheskali099
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Simplest GCD of two numbers (Python)

import math a = int(input("Enter first number: ")) b = int(input("Enter second


number: ")) gcd = [Link](a, b) print("GCD of", a, "and", b, "is", gcd)

Simplest code for exponential (power of a number)


base = float(input("Enter base: ")) exp = float(input("Enter exponent: ")) result =
base ** exp print(f"{base} raised to the power {exp} is {result})

Simplest code for maximum of a list of numbers


nums_input = input("Enter numbers separated by spaces: ") nums = list(map(float,
nums_input.split())) print("Maximum:", max(nums))

Simplest code for most frequent words in text read from file
from collections import Counter with open("[Link]", "r") as f: words =
[Link]().split() print(Counter(words).most_common())

Simplest linear search code


nums = [4, 7, 1, 9, 3] target = 9 for i in range(len(nums)): if nums[i] == target:
print("Found at index", i) break else: print("Not found")

Simplest insertion sort


nums = [5, 2, 4, 6, 1, 3] for i in range(1, len(nums)): key = nums[i] j = i - 1 while
j >= 0 and nums[j] > key: nums[j + 1] = nums[j] j -= 1 nums[j + 1] = key
print("Insertion Sort:", nums)

Simplest selection sort


nums = [5, 2, 4, 6, 1, 3] for i in range(len(nums)): min_idx = i for j in range(i +
1, len(nums)): if nums[j] < nums[min_idx]: min_idx = j nums[i], nums[min_idx] =
nums[min_idx], nums[i] print("Selection Sort:", nums)

Simplest merge sort


def merge(left, right): result = [] i = j = 0 while i < len(left) and j <
len(right): if left[i] < right[j]: [Link](left[i]) i += 1 else:
[Link](right[j]) j += 1 [Link](left[i:]) [Link](right[j:])
return result def merge_sort(arr): if len(arr) <= 1: return arr mid = len(arr) // 2
left = merge_sort(arr[:mid]) right = merge_sort(arr[mid:]) return merge(left, right)
nums = [5, 2, 4, 6, 1, 3] print(merge_sort(nums))

Simplest multiplication of matrix


A = [[1, 2, 3], [4, 5, 6]] B = [[7, 8], [9, 10], [11, 12]] result = [[0] * len(B[0])
for _ in range(len(A))] for i in range(len(A)): for j in range(len(B[0])): for k in
range(len(B)): result[i][j] += A[i][k] * B[k][j] for row in result: print(row)

You might also like