Python code to print a diamond pattern.

~\Documents\fullchip\python\mysite\webpages\templates\webpages\test.py.html
# import python modules
import sys
import os
import string
# Declare number of lines to be read this value should be odd
line = 15
print "print %s" % (line)
# Calculate centre of the diamond, also calculate the offset
centre = line/2
offset = line - 2*centre
if offset == 0:
        print "...Error the number of lines should be odd"
        sys.exit()
if offset == 1:
    centre = centre + 1
# Print statements
print "centre %s" %(centre)
print "--Here in the diamond for lines = %s--" % (line)
# Generate the diamond pattern
# Calculate spaces and stars for each line

for line_d in range (1,line+1,1):
    spaces = centre - line_d
    if spaces < 0:
        spaces = spaces * -1
    stars = 2*(centre - spaces) - 1
   diamond = ""
    for l in range (0, spaces, 1):
           diamond = diamond + " "
    for q in range (0, stars, 1):
           diamond = diamond + "*"
    print diamond

LTE - 4G Wireless Technology

Digital fundamentals.

Interview Questions.

Results of above code is printed below thorugh an image.

Python diamond pattern.

Following list of programs are discussed in this sec:

¶ File Operations: 

  1. Open and read file from within the python program as a single string. Command line arguments lists as string with ‘sys.argv’.
  2. Use of  glob module in Python code . Read file using classes, global variables and functions. Program to open and write file.
  3. Specify files to be written from the command line. File writes using classes and global variables

¶ Program to read file, filter text, conversion to HEX, conversion from Hexadecimal to Signed Magnitude.S Steps for conditional statements, converting string to hexadecimalvalue, strip of white space at end, converting 12 bit hexadecimal values to 12 bit signed values.

¶ Advanced python code to cover UNIX shell commands, read lines from a text file, ‘sysargv’ command line operations, use of class and multiple functions. 

¶ Write a program to generate diamond pattern.

¶ Implement Python function calls to different classes.

¶ Python read binary files, use of struct.unpack.

¶ Python error’s with complete code examples:-

  1. TypeError: __init__() takes exactly 2 arguments (1 given),
  2. TypeError: 'str' object is not callable, TypeError: not all arguments converted during string formatting,
  3. AttributeError: ** instance has no attribute **

 

 

Tutorials @fullchipdesign.com

Verilog Tutorial.

LTE Tutorial.

Memory Tutorial.

Hope you liked! this page. Don't forgot to access relevant previous and next sections with links below.