python
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python [2021/10/19 19:23] – [Data wrangling in Python] carl | python [2026/04/01 09:46] (current) – [Data wrangling in Python] carl | ||
|---|---|---|---|
| Line 53: | Line 53: | ||
| ==== Data wrangling in Python ==== | ==== Data wrangling in Python ==== | ||
| - | https:// | ||
| - | Prérequis : | + | Suivi les 25/11/2024 et 02/12/2024 à UGent : [[python: |
| - | * https://www.learnpython.org/en/Welcome | + | |
| - | < | + | Prérequis : [[python: |
| - | # -*- coding: utf-8 -*- | + | |
| - | """ | + | |
| - | Created | + | |
| - | @author: carl | ||
| - | """ | ||
| - | |||
| - | ### https:// | ||
| - | |||
| - | x = 1 | ||
| - | if x == 1 : | ||
| - | print(" | ||
| - | |||
| - | myint = 7 | ||
| - | print(myint) | ||
| - | |||
| - | myfloat = 7.0 | ||
| - | print(myfloat) | ||
| - | myfloat = float(7) | ||
| - | print(myfloat) | ||
| - | |||
| - | mystring = ' | ||
| - | print(mystring) | ||
| - | mystring = " | ||
| - | print(mystring) | ||
| - | |||
| - | mystring = " | ||
| - | print(mystring) | ||
| - | |||
| - | one = 1 | ||
| - | two = 2 | ||
| - | three = one + two | ||
| - | print(three) | ||
| - | |||
| - | hello = " | ||
| - | world = " | ||
| - | helloworld = hello + " " + world | ||
| - | print(helloworld) | ||
| - | |||
| - | a, b = 3, 4 | ||
| - | print(a,b) | ||
| - | |||
| - | # This will not work! | ||
| - | one = 1 | ||
| - | two = 2 | ||
| - | hello = " | ||
| - | |||
| - | print(one + two + hello) | ||
| - | |||
| - | mystring = " | ||
| - | myfloat = 10.0 | ||
| - | myint = 20 | ||
| - | |||
| - | # testing code | ||
| - | if mystring == " | ||
| - | print(" | ||
| - | if isinstance(myfloat, | ||
| - | print(" | ||
| - | if isinstance(myint, | ||
| - | print(" | ||
| - | | ||
| - | numbers = [] | ||
| - | strings = [] | ||
| - | names = [" | ||
| - | |||
| - | numbers.append(1) | ||
| - | numbers.append(2) | ||
| - | numbers.append(3) | ||
| - | strings.append(' | ||
| - | strings.append(' | ||
| - | |||
| - | second_name = names[1] | ||
| - | |||
| - | print(numbers) | ||
| - | print(strings) | ||
| - | print(" | ||
| - | |||
| - | # modulo | ||
| - | remainder = 11 % 3 | ||
| - | print(remainder) | ||
| - | |||
| - | squared = 7 ** 2 | ||
| - | cubed = 2 ** 3 | ||
| - | print(squared) | ||
| - | print(cubed) | ||
| - | |||
| - | lotsofhellos = (" | ||
| - | print(lotsofhellos) | ||
| - | |||
| - | even_numbers = [2,4,6,8] | ||
| - | odd_numbers = [1,3,5,7] | ||
| - | all_numbers = odd_numbers + even_numbers | ||
| - | print(all_numbers) | ||
| - | |||
| - | print([1, | ||
| - | |||
| - | x = object() | ||
| - | y = object() | ||
| - | |||
| - | # TODO: change this code | ||
| - | x_list = [x] * 10 | ||
| - | y_list = [y] * 10 | ||
| - | big_list = x_list + y_list | ||
| - | |||
| - | print(" | ||
| - | print(" | ||
| - | print(" | ||
| - | |||
| - | # testing code | ||
| - | if x_list.count(x) == 10 and y_list.count(y) == 10: | ||
| - | print(" | ||
| - | if big_list.count(x) == 10 and big_list.count(y) == 10: | ||
| - | print(" | ||
| - | | ||
| - | data = (" | ||
| - | format_string = " | ||
| - | |||
| - | print(" | ||
| - | |||
| - | data = (" | ||
| - | format_string = "Hello %s %s. Your current balance is $%s." | ||
| - | |||
| - | print(format_string % data) | ||
| - | |||
| - | astring = "Hello world!" | ||
| - | afewwords = astring.split(" | ||
| - | print(afewwords) | ||
| - | |||
| - | s = "Hey there! what should this string be?" | ||
| - | s = "Whot shoald thas be?" | ||
| - | # Length should be 20 | ||
| - | print(" | ||
| - | |||
| - | # First occurrence of " | ||
| - | print(" | ||
| - | |||
| - | # Number of a's should be 2 | ||
| - | print(" | ||
| - | |||
| - | # Slicing the string into bits | ||
| - | print(" | ||
| - | print(" | ||
| - | print(" | ||
| - | print(" | ||
| - | print(" | ||
| - | |||
| - | # Convert everything to uppercase | ||
| - | print(" | ||
| - | |||
| - | # Convert everything to lowercase | ||
| - | print(" | ||
| - | |||
| - | # Check how a string starts | ||
| - | if s.startswith(" | ||
| - | print(" | ||
| - | |||
| - | # Check how a string ends | ||
| - | if s.endswith(" | ||
| - | print(" | ||
| - | |||
| - | # Split the string into three separate strings, | ||
| - | # each containing only a word | ||
| - | print(" | ||
| - | |||
| - | numbers = [ | ||
| - | 951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, | ||
| - | 615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, | ||
| - | 386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, | ||
| - | 399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, | ||
| - | 815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, | ||
| - | 958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, | ||
| - | 743, 527 | ||
| - | ] | ||
| - | for number in numbers: | ||
| - | if number == 237: | ||
| - | break | ||
| - | |||
| - | if number % 2 == 1: | ||
| - | continue | ||
| - | |||
| - | print(number) | ||
| - | | ||
| - | |||
| - | # Modify this function to return a list of strings as defined above | ||
| - | def list_benefits(): | ||
| - | list = ["More organized code", "More readable code", " | ||
| - | return list | ||
| - | |||
| - | # Modify this function to concatenate to each benefit - " is a benefit of functions!" | ||
| - | def build_sentence(benefit): | ||
| - | sentence = benefit + " is a benefit of functions!" | ||
| - | return sentence | ||
| - | |||
| - | def name_the_benefits_of_functions(): | ||
| - | list_of_benefits = list_benefits() | ||
| - | for benefit in list_of_benefits: | ||
| - | print(build_sentence(benefit)) | ||
| - | |||
| - | name_the_benefits_of_functions() | ||
| - | |||
| - | class MyClass: | ||
| - | variable = " | ||
| - | |||
| - | def function(self): | ||
| - | print(" | ||
| - | |||
| - | myobjectx = MyClass() | ||
| - | |||
| - | myobjectx.variable | ||
| - | |||
| - | print(myobjectx.variable) | ||
| - | |||
| - | myobjectx = MyClass() | ||
| - | myobjecty = MyClass() | ||
| - | |||
| - | myobjecty.variable = " | ||
| - | |||
| - | # Then print out both values | ||
| - | print(myobjectx.variable) | ||
| - | print(myobjecty.variable) | ||
| - | |||
| - | myobjectx.function() | ||
| - | |||
| - | |||
| - | # define the Vehicle class | ||
| - | class Vehicle: | ||
| - | name = "" | ||
| - | kind = " | ||
| - | color = "" | ||
| - | value = 100.00 | ||
| - | def description(self): | ||
| - | desc_str = "%s is a %s %s worth $%.2f." | ||
| - | return desc_str | ||
| - | # your code goes here | ||
| - | car1 = Vehicle() | ||
| - | car2 = Vehicle() | ||
| - | car1.kind = " | ||
| - | car1.color = " | ||
| - | car1.value = 600000.00 | ||
| - | car1.name = " | ||
| - | car2.kind = " | ||
| - | car2.color = " | ||
| - | car2.value = 10000.00 | ||
| - | car2.name = " | ||
| - | |||
| - | # test code | ||
| - | print(car1.description()) | ||
| - | print(car2.description()) | ||
| - | |||
| - | |||
| - | phonebook = {} | ||
| - | phonebook[" | ||
| - | phonebook[" | ||
| - | phonebook[" | ||
| - | print(phonebook) | ||
| - | |||
| - | phonebook = { | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | } | ||
| - | print(phonebook) | ||
| - | |||
| - | phonebook = {" | ||
| - | for name, number in phonebook.items(): | ||
| - | print(" | ||
| - | | ||
| - | del phonebook[" | ||
| - | print(phonebook) | ||
| - | |||
| - | phonebook = { | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | } | ||
| - | phonebook.pop(" | ||
| - | print(phonebook) | ||
| - | |||
| - | phonebook = { | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | } | ||
| - | # your code goes here | ||
| - | phonebook[" | ||
| - | del phonebook[" | ||
| - | |||
| - | # testing code | ||
| - | if " | ||
| - | print(" | ||
| - | | ||
| - | if " | ||
| - | print(" | ||
| - | | ||
| - | import re | ||
| - | |||
| - | find_members = [] | ||
| - | for member in dir(re): | ||
| - | if " | ||
| - | find_members.append(member) | ||
| - | |||
| - | print(sorted(find_members)) | ||
| - | </ | ||
python.1634671406.txt.gz · Last modified: by carl
