Skip to main content

Input 2D Matrix

0 likes • Nov 19, 2022 • 0 views
Python
Loading...

More Python Posts

print indices

0 likes • Nov 18, 2022 • 0 views
Python
# List
lst = [1, 2, 3, 'Alice', 'Alice']
# One-Liner
indices = [i for i in range(len(lst)) if lst[i]=='Alice']
# Result
print(indices)
# [3, 4]

convert bytes to a string

0 likes • Jun 1, 2023 • 0 views
Python
bytes_data = b'Hello, World!'
string_data = bytes_data.decode('utf-8')
print("String:", string_data)

Python Fibonacci

CHS
0 likes • Sep 6, 2020 • 0 views
Python
def Fibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
return 0
# Second Fibonacci number is 1
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
# Driver Program
print(Fibonacci(9))

Currency Converter

0 likes • Nov 19, 2022 • 0 views
Python
""" Currency Converter
----------------------------------------
"""
import urllib.request
import json
def currency_converter(currency_from, currency_to, currency_input):
yql_base_url = "https://query.yahooapis.com/v1/public/yql"
yql_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair' \
'%20in%20("'+currency_from+currency_to+'")'
yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
try:
yql_response = urllib.request.urlopen(yql_query_url)
try:
json_string = str(yql_response.read())
json_string = json_string[2:
json_string = json_string[:-1]
print(json_string)
yql_json = json.loads(json_string)
last_rate = yql_json['query']['results']['rate']['Rate']
currency_output = currency_input * float(last_rate)
return currency_output
except (ValueError, KeyError, TypeError):
print(yql_query_url)
return "JSON format error"
except IOError as e:
print(str(e))
currency_input = 1
#currency codes : http://en.wikipedia.org/wiki/ISO_4217
currency_from = "USD"
currency_to = "TRY"
rate = currency_converter(currency_from, currency_to, currency_input)
print(rate)

Hello, python

0 likes • Jan 20, 2021 • 2 views
Python
print(“Hello World”)

Check Armstrong Number

0 likes • May 31, 2023 • 0 views
Python
# Function to check Armstrong number
def is_armstrong_number(number):
# Convert number to string to iterate over its digits
num_str = str(number)
# Calculate the sum of the cubes of each digit
digit_sum = sum(int(digit) ** len(num_str) for digit in num_str)
# Compare the sum with the original number
if digit_sum == number:
return True
else:
return False
# Prompt user for a number
number = int(input("Enter a number: "))
# Check if the number is an Armstrong number
if is_armstrong_number(number):
print(number, "is an Armstrong number.")
else:
print(number, "is not an Armstrong number.")