Difference between revisions of "Python Play"
Jump to navigation
Jump to search
| (9 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | # | + | # Palidrome |
| + | - https://www.mygreatlearning.com/blog/palindrome-in-python/ | ||
| + | ``` | ||
| + | def is_palidrome(string): | ||
| + | string_alpha = "" | ||
| + | for char in string: | ||
| + | if char.isalpha(): | ||
| + | string_alpha += char | ||
| + | string_alpha_lower = string_alpha.lower() | ||
| + | if(string_alpha_lower == string_alpha_lower[::-1]): | ||
| + | print(f'The string "{string_alpha_lower}" is a palindrome.') | ||
| + | return True | ||
| + | else: | ||
| + | print(f'The string "{string_alpha_lower}" is not a palindrome.') | ||
| + | return False | ||
| + | |||
| + | |||
| + | # string = input(("Enter a string:")) | ||
| + | string = "No, Hannah, 11 !! on" | ||
| + | |||
| + | is_palidrome(string) | ||
| + | ``` | ||
| + | You can use reversed() but ::-1 is better IMO | ||
| + | |||
| + | |||
| + | |||
| + | # 1a | ||
| + | |||
| + | ``` | ||
| + | query = {'lat':'45', 'lon':'180'} | ||
| + | r = requests.get('http://api.open-notify.org/iss-pass.json', params=query) | ||
| + | |||
| + | data = r.json() | ||
| + | |||
| + | for k,v in data.items(): | ||
| + | print(k, v) | ||
| + | |||
| + | for i in data['people']: | ||
| + | print(i['craft']) | ||
| + | ``` | ||
| + | |||
| + | https://www.nylas.com/blog/use-python-requests-module-rest-apis/ | ||
| + | ``` | ||
| + | query = {'lat':'45', 'lon':'180'} | ||
| + | r = requests.get('http://api.open-notify.org/iss-pass.json', params=query) | ||
| + | print(r.json()) | ||
| + | print(r.content, r.text, r.json) | ||
| + | print(r.headers["date"]) | ||
| + | |||
| + | ``` | ||
| + | |||
| + | ``` | ||
| + | r = requests.post('https://httpbin.org/post', data = {'key':'value'}) | ||
| + | requests.put('https://httpbin.org/put', data = {'key':'value'}) | ||
| + | ``` | ||
| + | |||
| + | # 2a | ||
``` | ``` | ||
ld = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}] | ld = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}] | ||
| Line 8: | Line 64: | ||
if k == 'a': | if k == 'a': | ||
print(i) | print(i) | ||
| + | |||
| + | [d for d in ld][1] | ||
``` | ``` | ||
Latest revision as of 16:42, 2 October 2021
Palidrome
def is_palidrome(string):
string_alpha = ""
for char in string:
if char.isalpha():
string_alpha += char
string_alpha_lower = string_alpha.lower()
if(string_alpha_lower == string_alpha_lower[::-1]):
print(f'The string "{string_alpha_lower}" is a palindrome.')
return True
else:
print(f'The string "{string_alpha_lower}" is not a palindrome.')
return False
# string = input(("Enter a string:"))
string = "No, Hannah, 11 !! on"
is_palidrome(string)
You can use reversed() but ::-1 is better IMO
1a
query = {'lat':'45', 'lon':'180'}
r = requests.get('http://api.open-notify.org/iss-pass.json', params=query)
data = r.json()
for k,v in data.items():
print(k, v)
for i in data['people']:
print(i['craft'])
https://www.nylas.com/blog/use-python-requests-module-rest-apis/
query = {'lat':'45', 'lon':'180'}
r = requests.get('http://api.open-notify.org/iss-pass.json', params=query)
print(r.json())
print(r.content, r.text, r.json)
print(r.headers["date"])
r = requests.post('https://httpbin.org/post', data = {'key':'value'})
requests.put('https://httpbin.org/put', data = {'key':'value'})
2a
ld = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}]
for d in ld:
for i in d:
if k == 'a':
print(i)
[d for d in ld][1]
1
[d['value'] for d in l]
2
list1 = ["a", "b" ,"c"]
dictionary1 = {"a":1, "b":2, "c":3}
3
[d['value'] for d in l]
4
def to_dictionary(keys, values):
return dict(zip(keys, values))
keys = ["a", "b", "c"]
values = [2, 3, 4]
print(to_dictionary(keys, values)) # {'a': 2, 'c': 4, 'b': 3}
5
[d['value'] for d in l if 'value' in d]
6
a
7
a