Skip to content

Instantly share code, notes, and snippets.

@MarketaP
Created January 24, 2019 19:49
Show Gist options
  • Save MarketaP/fa3a9da0e9f4f68bd70f83062eb124d2 to your computer and use it in GitHub Desktop.
Save MarketaP/fa3a9da0e9f4f68bd70f83062eb124d2 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
my_name = "Marketa Podebradska"
my_name2 = 'Marketa'
print(my_name, my_name2)
# Coercion
day_length = "24"
print(float(day_length))
print(int(day_length))
print(str(day_length))
# Using backslash in string
print("This is Bob's house")
print('This is Bob\'s house')
print("Marketa\nPodebradska")
print("(1)\t Marketa Podebradska")
my_path = r"C:\Users\mpodebradska2\IDLWorkspace\Default"
print(my_path)
import os
my_path = r"C:\Users\mpodebradska2\IDLWorkspace\Default" + os.path.sep
print(my_path)
# Working with strings
result = my_name.startswith("M")
print(result)
print(my_name[8:])
print(my_name[my_name.find("P"):])
for letter in my_name:
if letter.lower() == "p":
print(letter)
data = " 08/24/2010 0800 $156,000,000.00"
dollars = float(data.split()[-1]\
.replace("$", "")\
.replace(",", "")\
)
print(dollars)
banana = "banana"
count = banana.count("a")
print(count)
number = len([letter for letter in banana if letter == "a"])
print(number)
# List comprehension
ages = [5, 25, 30, 50, 100]
new_age = [a + 5 if a <= 30 else a for a in ages]
print(new_age)
# Dictionaries
nres = {"name" : "NRES 898/498",
"students" : {
"Marketa": {"grade" : 100}
}
}
print(nres)
for key, value in nres.items():
print(key, value)
# Dictionary comprehensions
example_dict = {"Joe" : 35, "Bob" : 50}
example_dict.update({"Marketa" : 28})
new_dict = {key : value + 1 for key, value in example_dict.items()}
print(new_dict)
#Exercxise 4
string = "50.0, 500, 6"
def func(string, delimiter = ","):
'''Takes a string with delimited numbers and returns a list pf float values'''
num = string.split(delimiter)
return [float(number) for number in num]
print(func(string))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment