Skip to content

Instantly share code, notes, and snippets.

@atifazad
Last active February 16, 2020 17:18
Show Gist options
  • Save atifazad/9b12091df64703904ef89fa4e5b2aadb to your computer and use it in GitHub Desktop.
Save atifazad/9b12091df64703904ef89fa4e5b2aadb to your computer and use it in GitHub Desktop.
Python programming basics
Python basics step by step examples
"""
Execute this file with following command in terminal
> python step_1_sequence.py
"""
print('Table of number 2')
print('2 x 1 = 2')
print('2 x 2 = 4')
print('2 x 3 = 6')
print('2 x 4 = 8')
print('2 x 5 = 10')
print('2 x 6 = 12')
print('2 x 7 = 14')
print('2 x 8 = 16')
print('2 x 9 = 18')
print('2 x 10 = 20')
"""
Execute this file with following command in terminal
> python step_2_variables_and_expressions.py
"""
num = 2
print('Table of number {}'.format(num))
print('{} x 1 = {}'.format(num, num*1))
print('{} x 2 = {}'.format(num, num*2))
print('{} x 3 = {}'.format(num, num*3))
print('{} x 4 = {}'.format(num, num*4))
print('{} x 5 = {}'.format(num, num*5))
print('{} x 6 = {}'.format(num, num*6))
print('{} x 7 = {}'.format(num, num*7))
print('{} x 8 = {}'.format(num, num*8))
print('{} x 9 = {}'.format(num, num*9))
print('{} x 10 = {}'.format(num, num*10))
"""
Execute this file with following command in terminal
> python step_3_iteration.py
"""
num = 4
print('Table of number {}'.format(num))
for i in range(1, 11):
print('{} x {} = {}'.format(num, i, num*i) )
"""
Execute this file with following command in terminal
> python step_4_input.py
"""
num = int(input('Number to generate table: '))
for i in range(1, 11):
print('{} x {} = {}'.format(num, i, num*i) )
"""
Execute this file with following command in terminal
> python step_5_selection.py
"""
num = input('Number to generate table: ')
if num.isnumeric() == True:
num = int(num)
for i in range(1, 11):
print('{} x {} = {}'.format(num, i, num*i) )
else:
print('Wrong input')
"""
Execute this file with following command in terminal
> python step_6_repeat_until_user_terminates.py
"""
terminate = False
while terminate == False:
num = input('Number to generate table: ')
if num.isnumeric() == True:
num = int(num)
for i in range(1, 11):
print('{} x {} = {}'.format(num, i, num*i) )
else:
print('Wrong input')
more = input('Do you want to continue with another number (Y=yes or any other key to terminate)? ')
terminate = True if more not in ['y', 'Y'] else False
"""
Execute this file with following command in terminal
> python step_7_functions.py
"""
def execute():
terminate = False
while terminate == False:
num = input('Number to generate table: ')
if num.isnumeric() == True:
num = int(num)
print_table(num)
else:
print('Wrong input')
terminate = should_terminate()
def print_table(num):
for i in range(1, 11):
print('{} x {} = {}'.format(num, i, num*i) )
def should_terminate():
more = input('Do you want to continue with another number (Y=yes or any other key to terminate)? ')
terminate = True if more not in ['y', 'Y'] else False
return terminate
execute()
"""
Execute this file with following command in terminal
> python step_8_code_docs.py
"""
def execute():
"""
takes input and prints table for that number
continues input and output until the user terminates the execution.
"""
terminate = False
while terminate == False:
num = input('Number to generate table: ')
if num.isnumeric() == True:
num = int(num)
print_table(num)
else:
print('Wrong input')
terminate = should_terminate()
def print_table(num):
"""
prints the table of number 'num'
:param num: number of which the table is to be printed
:type num: int
"""
for i in range(1, 11):
print('{} x {} = {}'.format(num, i, num*i) )
def should_terminate():
"""
Prompts user if or not he wants to continue executing the program
and takes the input Y/y for yes and any other key for no
:return: should the program terminate
:rtype: bool
"""
more = input('Do you want to continue with another number (Y=yes or any other key to terminate)? ')
terminate = True if more not in ['y', 'Y'] else False
return terminate
execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment