How to Create Multiplication Table, Square, Import Turtle, Fibonacci Sequence, Letter Conversion, Inverted Pyramid, Math Equation Programs in Python
Example of Multiplication Table using Python
#here is another example of program
#using python programming language
#multiplication table:
print(' Multiplication Table\n',
'--------------------\n')
n = int(input('n = '))
print(' ',end = ' ')
for coloumn in range(1, n+1):
print('%3d ' % coloumn, end = '')
print() #movetherow
for row in range(1, n + 1):
print('%3d ' % row, end = '')
for coloumn in range(1, n + 1):
print('%3d ' % (row * coloumn), end = '')
print()
How to create a square / box using Python
#how to create a box (example)
n = int(input("Input a number = "))
character = input("Input a character = ")
print()
#process
for coloumn in range(1, 2 * n+1):
print(character, end = "")
print()
for index in range(1, n-1):
print(character, end = "")
for coloumn in range(1, 2 * n-1):
print(" ", end = "") #for space
print(character)
for coloumn in range(1, 2 * n + 1):
print(character, end = "")
print()
Example of program using import turtle in Python
#another source code of pentagon abal-abal wkwk
import turtle
t = turtle.Turtle()
t.color("green")
t.speed(0)
def draw_pentagon():
for side in range(5):
t.backward(5 * side)
t.left(360 / 5)
for side in range(10):
t.backward(5 * side)
t.left(360 / 5)
for triangle in range(5):
draw_pentagon()
t.forward(5 * side)
t.right(360 / 5)
Fibonacci Sequence using Python
#Fibonacci Sequence
print('|------------------|')
print('|Fibonacci Sequence|')
print('|------------------|')
print()
n = int(input(' Enter n value = '))
print()
fn1 = 1
fn2 = 1
print(1, end = ' ')
print(1, end = ' ')
fn = fn1 + fn2
index = 3
while index <= n:
print(fn, end = ' ')
fn1 = fn2
fn2 = fn
fn = fn1 + fn2
index = index + 1
print()
Python String Method example to convert a letter or sentence
#letter conversion
sentence = input(' input a sentence : ')
print('\n','After Conversion',end = '\n')
print(' upper :', sentence.upper())
print(' lower :', sentence.lower())
print(' capitalize:', sentence.capitalize())
print(' title :', sentence.title())
print(' swapcase :', sentence.swapcase())
Inverted Pyramid in Python
#This is the example
#how to make an inversion triangle
#with python programming language
n = int(input('n = '))
ch = input('Input a character = ')
for row in range(1, n+1):
#for space
for coloumn in range(1, row):
print(' ', end = '')
#show the character
for index in range(1, 2 * (n - row)):
print(ch, end = '')
print()
Example of mathematical equation in Python
This is just an example, we will input the x value to get the y value as the output.
#Example:
print("y = 4x^3 - 2x^2 + 6x - 2") #this is the question
#to input the value of x:
x = int(input("x = "))
#to get the output or y:
y = 4 * x ** 3 - 2 * x ** 2 + 6 * x - 2
print("y =",y)