User Tools

Site Tools


python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python [2021/10/19 19:23] – [Data wrangling in Python] carlpython [2026/04/01 09:46] (current) – [Data wrangling in Python] carl
Line 53: Line 53:
  
 ==== Data wrangling in Python ==== ==== Data wrangling in Python ====
-https://jorisvandenbossche.github.io/FLAMES-python-data-wrangling/setup.html 
  
-Prérequis : +Suivi les 25/11/2024 et 02/12/2024 à UGent : [[python:datawrangling|Data Wrangling in Python]]
-  * https://www.learnpython.org/en/Welcome+
  
-<code python>#!/usr/bin/env python3 +Prérequis : [[python:learnpython|Learn Python]] ou [[https://lectures.scientific-python.org/intro/language/python_language.html|the chapter on the Python language from the Scipy Lecture Notes]]
-# -*- coding: utf-8 -*- +
-""" +
-Created on Fri Oct 15 13:21:07 2021+
  
-@author: carl 
-""" 
- 
-### https://www.learnpython.org/en/  ### 
- 
-x = 1 
-if x == 1 : 
-        print("Hello, World!") 
- 
-myint = 7 
-print(myint) 
- 
-myfloat = 7.0 
-print(myfloat) 
-myfloat = float(7) 
-print(myfloat) 
- 
-mystring = 'hello' 
-print(mystring) 
-mystring = "hello" 
-print(mystring) 
- 
-mystring = "Don't worry about apostrophes" 
-print(mystring) 
- 
-one = 1 
-two = 2 
-three = one + two 
-print(three) 
- 
-hello = "hello" 
-world = "world" 
-helloworld = hello + " " + world 
-print(helloworld) 
- 
-a, b = 3, 4 
-print(a,b) 
- 
-# This will not work! 
-one = 1 
-two = 2 
-hello = "hello" 
- 
-print(one + two + hello) 
- 
-mystring = "hello" 
-myfloat = 10.0 
-myint = 20 
- 
-# testing code 
-if mystring == "hello": 
-    print("String: %s" % mystring) 
-if isinstance(myfloat, float) and myfloat == 10.0: 
-    print("Float: %f" % myfloat) 
-if isinstance(myint, int) and myint == 20: 
-    print("Integer: %d" % myint) 
-     
-numbers = [] 
-strings = [] 
-names = ["John", "Eric", "Jessica"] 
- 
-numbers.append(1) 
-numbers.append(2) 
-numbers.append(3) 
-strings.append('hello') 
-strings.append('world') 
- 
-second_name = names[1] 
- 
-print(numbers) 
-print(strings) 
-print("The second name on the names list is %s" % second_name) 
- 
-# modulo 
-remainder = 11 % 3 
-print(remainder) 
- 
-squared = 7 ** 2 
-cubed = 2 ** 3 
-print(squared) 
-print(cubed) 
- 
-lotsofhellos = ("hello" + " ") * 10 
-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,2,3] * 3) 
- 
-x = object() 
-y = object() 
- 
-# TODO: change this code 
-x_list = [x] * 10 
-y_list = [y] * 10 
-big_list = x_list + y_list 
- 
-print("x_list contains %d objects" % len(x_list)) 
-print("y_list contains %d objects" % len(y_list)) 
-print("big_list contains %d objects" % len(big_list)) 
- 
-# testing code 
-if x_list.count(x) == 10 and y_list.count(y) == 10: 
-    print("Almost there...") 
-if big_list.count(x) == 10 and big_list.count(y) == 10: 
-    print("Great!") 
-     
-data = ("John", "Doe", 53.44) 
-format_string = "Hello" 
- 
-print("%s %s %s. Your current balance is $%f" % (format_string, data[0], data[1], data [2])) 
- 
-data = ("John", "Doe", 53.44) 
-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("Length of s = %d" % len(s)) 
- 
-# First occurrence of "a" should be at index 8 
-print("The first occurrence of the letter a = %d" % s.index("a")) 
- 
-# Number of a's should be 2 
-print("a occurs %d times" % s.count("a")) 
- 
-# Slicing the string into bits 
-print("The first five characters are '%s'" % s[:5]) # Start to 5 
-print("The next five characters are '%s'" % s[5:10]) # 5 to 10 
-print("The thirteenth character is '%s'" % s[12]) # Just number 12 
-print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing) 
-print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end 
- 
-# Convert everything to uppercase 
-print("String in uppercase: %s" % s.upper()) 
- 
-# Convert everything to lowercase 
-print("String in lowercase: %s" % s.lower()) 
- 
-# Check how a string starts 
-if s.startswith("Str"): 
-    print("String starts with 'Str'. Good!") 
- 
-# Check how a string ends 
-if s.endswith("ome!"): 
-    print("String ends with 'ome!'. Good!") 
- 
-# Split the string into three separate strings, 
-# each containing only a word 
-print("Split the words of the string: %s" % s.split(" ")) 
- 
-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", "Easier code reuse", "Allowing programmers to share and connect code together"] 
-    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 = "blah" 
- 
-    def function(self): 
-        print("This is a message inside the class.") 
- 
-myobjectx = MyClass() 
- 
-myobjectx.variable 
- 
-print(myobjectx.variable) 
- 
-myobjectx = MyClass() 
-myobjecty = MyClass() 
- 
-myobjecty.variable = "yackity" 
- 
-# Then print out both values 
-print(myobjectx.variable) 
-print(myobjecty.variable) 
- 
-myobjectx.function() 
- 
- 
-# define the Vehicle class 
-class Vehicle: 
-    name = "" 
-    kind = "car" 
-    color = "" 
-    value = 100.00 
-    def description(self): 
-        desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value) 
-        return desc_str 
-# your code goes here 
-car1 = Vehicle() 
-car2 = Vehicle() 
-car1.kind = "convertible" 
-car1.color = "red" 
-car1.value = 600000.00 
-car1.name = "Fer" 
-car2.kind = "van" 
-car2.color = "blue" 
-car2.value = 10000.00 
-car2.name = "Jump" 
- 
-# test code 
-print(car1.description()) 
-print(car2.description()) 
- 
- 
-phonebook = {} 
-phonebook["John"] = 938477566 
-phonebook["Jack"] = 938377264 
-phonebook["Jill"] = 947662781 
-print(phonebook) 
- 
-phonebook = { 
-    "John" : 938477566, 
-    "Jack" : 938377264, 
-    "Jill" : 947662781 
-} 
-print(phonebook) 
- 
-phonebook = {"John" : 938477566,"Jack" : 938377264,"Jill" : 947662781} 
-for name, number in phonebook.items(): 
-    print("Phone number of %s is %d" % (name, number)) 
-     
-del phonebook["John"] 
-print(phonebook) 
- 
-phonebook = { 
-   "John" : 938477566, 
-   "Jack" : 938377264, 
-   "Jill" : 947662781 
-} 
-phonebook.pop("John") 
-print(phonebook) 
- 
-phonebook = {   
-    "John" : 938477566, 
-    "Jack" : 938377264, 
-    "Jill" : 947662781 
-}   
-# your code goes here 
-phonebook["Jake"] = 938273443 
-del phonebook["Jill"] 
- 
-# testing code 
-if "Jake" in phonebook:   
-    print("Jake is listed in the phonebook.") 
-     
-if "Jill" not in phonebook:       
-    print("Jill is not listed in the phonebook.") 
-     
-import re 
- 
-find_members = [] 
-for member in dir(re): 
-    if "find" in member: 
-        find_members.append(member) 
- 
-print(sorted(find_members)) 
-</code> 
python.1634671406.txt.gz · Last modified: by carl