Building fibonacci list(and more) – Python

Fibonacci list:
This program displays first x Fibonacci numbers , Where x is a number that is requested to enter the program.

Used: if , elif , else , for

fibonacci.png
To copy or see all commands you can click on Details button below

 

#!/usr/bin/python
”’
author: Hopeless
task: Fibonacci write a program that will print n items of a fibonacci list
”’
print ‘\nthis program will show you fibonacci list’

num = input (‘Enter a number, it will be the number of characters in the fibonacci list: ‘)
fibonacci = [1,1]
index=0
if num == 0:
print “fibonacci \n[]”
elif num == 1:
print “fibonacci \n[1]”
else:
for i in range(2,num):
index1=index+1
num1 = fibonacci[index]
num2 = fibonacci[index1]
num3 = num1+num2
fibonacci.append(num3)
index+=1
print “fibonacci”
print fibonacci

 

Untitled.png



fibonacci up number:

you enter a number and the program prints fibonacci list up to a given number

Used: while

fibonaccimax.png
To copy or see all commands you can click on Details button below

 

#!/usr/bin/python
”’
author: Hopeless
task: print fibonacci list up to a given number
”’

a,b=1,1
fibonacci = [1,1]

max = input (‘Enter a number, it will be the max number in the fibonacci list: ‘)

while a+b <=max:
a,b =b,a+b
fibonacci.append(b)

print “fibonacci list”
print fibonacci

 

Untitled2.png



fibonacci range:

you enter a minimum number , and maximum number .
program prints lists numbers between high number of low number

Used : while , if  ,  not in

fibonacci range.png

To copy or see all commands you can click on Details button below

 

#!/usr/bin/python
”’
author: Hopeless
task: print fibonacci within a given range (ex: 1,000-10,000)
”’
print “This program checks the list of Fibonacci, enter a min number and max number , range (ex: 1,000-10,000).”
nummin = input (‘Enter a number, it will be the min number of the range in fibonacci list: ‘)
nummax = input (‘Enter a number, it will be the max number of the range in fibonacci list: ‘)
fibonacci = [1,1]
index=0
num = 1

while num < nummax:

index1=index+1
index2 =index+2
num1 = fibonacci[index]
num2 = fibonacci[index1]

num = num1+num2
if num < nummax:
fibonacci.append(num)
index+=1
while nummin not in fibonacci:
nummin+=1

print “fibonacci range list”
fibonacci.index(nummin)
print fibonacci[fibonacci.index(nummin):]

 

Untitled3.png

Building counts lines , words and chars – Python

Program counts lines, words and chars
Here are two options

First option

Used: for , if

counts.png
To copy or see all commands you can click on Details button below

 

#!/usr/bin/python
”’
author: Hopeless
task: Write a program that counts lines, words and chars in a given file
”’
file = raw_input(‘please enter file name and locate if need:’)fileo = open(file,’r+’)
chars=0
words=0
lines=0

# calculate number of words
fileo = open(file,’r+’)
for word in fileo.read().split():
if word != ‘ ‘:
if word != ‘\n’:
words+=1
#print word
fileo.close()

# calculate number of chars
fileo = open(file,’r+’)
for word in fileo.read():
if word != ‘ ‘:
if word != ‘\n’:
chars+=1
fileo.close()

# calculate number of lines
fileo = open(file,’r+’)
for word in fileo.read().split(“\n”):
#print word
lines+=1
fileo.close()

print ‘number of words in file :’, words
print ‘number of chars in file :’, chars
print ‘number of lines in file :’, lines

 

First we have to download a file that you can work on it.
Then we run the Python file
And put the name of the file for inspection when requested to do so

Untitled.png

second option

Used: import sys , if , try , except , else

counts-g.png
To copy or see all commands you can click on Details button below

 

#!/usr/bin/python
”’
author: Hopeless
task: Write a program that counts lines, words and chars in a given file
”’
import sys# file = raw_input(‘please enter file name and locate if need:’)

if len(sys.argv) > 1:
try:
filex = open(sys.argv[1],’r’)
text = filex.read()
filex.close()

#print # of lines
print ‘Lines:’, text.count(‘\n’)+1

#print #of words
wordcount = text.split(None)
print ‘Words:’, len(wordcount)

#print # of chars
print ‘Chars:’, len(text)

except Exception, e:
print ‘I can\’t find the file, maybe try full path?…’
exit(0)

else:
print ‘Hi this prog it like linux wc…’
print ‘Usage: checkfilesgil.py [filename]\n’
print ‘Example: checkfilesgil.py sheker.txt’

 

First we have to download a file that you can work on it.
Then we run the Python file And that line we will put the filename

Untitled1.png

Building Calculator – Python

How to build a calculator ?
For example, building a simple calculator
Which performs an addition, subtraction and multiplication and division

Used: if , elif , else

calculator.pngTo copy or see all commands you can click on Details button below

 

#!/usr/bin/python
”’
author: Hopeless
task: Calculator to calculate the + – * /
”’

print
print(“This calculator can perform these operations”)
print(“Add +”)
print(“Subtract -“)
print(“Multiply *”)
print(“Divide /”)

num1 = input (‘ enter your first number : ‘)
calc1 = raw_input (‘ Select operation : ‘)
num2 = input (‘ enter your second number : ‘)

if calc1 == ‘+’:
print(str(num1)+’+’+str(num2)+’=’ +str(num1+num2))

elif calc1 == ‘-‘:
print(str(num1)+’-‘+str(num2)+’=’ +str(num1-num2))

elif calc1 == ‘*’:
print(str(num1)+’*’+str(num2)+’=’ +str(num1*num2))

elif calc1 == ‘/’:
print(str(num1)+’/’+str(num2)+’=’ +str(float(num1)/float(num2)))

else:
print(“Invalid input”)

 

calc.png