{"id":19,"date":"2023-09-13T09:19:39","date_gmt":"2023-09-13T09:19:39","guid":{"rendered":"http:\/\/harvard-open-data-project.local\/?page_id=19"},"modified":"2023-09-13T09:19:41","modified_gmt":"2023-09-13T09:19:41","slug":"python-data-wrangling","status":"publish","type":"page","link":"http:\/\/harvard-open-data-project.local\/python-data-wrangling\/","title":{"rendered":"Python Data Wrangling"},"content":{"rendered":"\n

This article is an introduction to Python for beginners, with the aim of equipping you with the basic knowledge and tools you need to explore and solve more complex problems.<\/p>\n\n\n\n

Understanding Variables and Operations<\/h2>\n\n\n\n

In Python, a variable is a placeholder for storing a value. The value of a variable can be changed, and it can be of different types, such as integers, floats, strings, or booleans.
For instance, if you want to declare a variable y<\/code> with a value of 3.45<\/code>, you would use the following Python code:<\/p>\n\n\n

y = 3.45\n<\/code><\/span><\/pre>\n\n\n

To print the value of a variable, you use the print<\/code> function:<\/p>\n\n\n

print<\/span>(y) # this will print 3.45<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

There are four commonly used types of variables in Python:<\/p>\n\n\n\n

    \n
  1. Integers (int)<\/code>: These are whole numbers such as 1, 2, 3, and so on.<\/li>\n\n\n\n
  2. Strings (str)<\/code>: These are sequences of characters enclosed in single or double quotes (\u2018hello\u2019, \u201cworld\u201d).<\/li>\n\n\n\n
  3. Booleans (bool)<\/code>: These are two possible values, True or False.<\/li>\n\n\n\n
  4. Floats (float)<\/code>: These are real numbers with a decimal point such as 3.14159.
    You can convert from one type to another using the built-in Python functions int()<\/code>, str()<\/code>, bool()<\/code>, and float()<\/code>.
    Python also allows various operations on variables, such as addition, subtraction, multiplication, division, and modulus among others. For example:<\/li>\n<\/ol>\n\n\n
    x = 7<\/span>\ny = 2<\/span>\nprint<\/span>(x+y)  # Addition; this will print 9<\/span>\nprint<\/span>(x-y)  # Subtraction; this will print 5<\/span>\nprint<\/span>(x\/y)  # Division; this will print 3.5<\/span>\nprint<\/span>(x%y)  # Modulus; this will print 1<\/span>\nprint<\/span>(x**y) # Exponentiation; this will print 49<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    Working with Strings<\/h2>\n\n\n\n

    In Python, strings are sequences of characters. They are defined by enclosing the characters in single (\u2019 ‘) or double (” “) quotes. Strings in Python are treated as lists, and therefore, you can perform operations on them like you would on a list.
    You can concatenate, or join, two strings using the +<\/code> operator:<\/p>\n\n\n

    print<\/span>(\"Hello\"<\/span> + \" \"<\/span> + \"World\"<\/span>)  # this will print \"Hello World\"<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    If you want to include quotation marks within a string, you can do so by using a backslash (\\<\/code>) before the quotation mark:<\/p>\n\n\n

    print<\/span>(\"He said, \\\"Hello.\\\"\"<\/span>)  # this will print He said, \"Hello.\"<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    Moreover, you can perform indexing on strings, which means you can access any character in a string by referring to its position inside the string. Remember that Python indexes start at 0.<\/p>\n\n\n

    my_string = \"Hello World\"<\/span>\nprint<\/span>(my_string[0<\/span>])  # this will print 'H'<\/span>\nprint<\/span>(my_string[-1<\/span>])  # this will print 'd'<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    Python also provides various methods to manipulate strings. For example, you can convert a string to lowercase or uppercase using the lower()<\/code> and upper()<\/code> methods, respectively:<\/p>\n\n\n

    my_string = \"Hello World\"<\/span>\nprint<\/span>(my_string.lower())  # this will print 'hello world'<\/span>\nprint<\/span>(my_string.upper())  # this will print 'HELLO WORLD'<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    You can also split a string into a list of substrings using the split()<\/code> method:<\/p>\n\n\n

    my_string = \"Hello World\"<\/span>\nprint<\/span>(my_string.split(\" \"<\/span>))  # this will print ['Hello', 'World']<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    Understanding Lists and List Methods<\/h2>\n\n\n\n

    Lists in Python are used to store multiple items in a single variable. Lists are ordered, mutable, and allow duplicate values.
    To declare a list, you use square brackets []<\/code>:<\/p>\n\n\n

    my_list = [\"apple\"<\/span>, \"banana\"<\/span>, \"cherry\"<\/span>]\n<\/code><\/span>Code language:<\/span> JavaScript<\/span> (<\/span>javascript<\/span>)<\/span><\/small><\/pre>\n\n\n

    You can access elements in a list by referring to their index number:<\/p>\n\n\n

    print<\/span>(my_list[0<\/span>])  # this will print 'apple'<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    You can add elements to a list using the append()<\/code> method, or insert an element at a specific position using the insert()<\/code> method:<\/p>\n\n\n

    my_list<\/span>.append<\/span>(\"dragonfruit<\/span>\")\nprint<\/span>(my_list<\/span>)  # this<\/span> will<\/span> print<\/span> ['apple'<\/span>, 'banana'<\/span>, 'cherry'<\/span>, 'dragonfruit'<\/span>]<\/span>\nmy_list<\/span>.insert<\/span>(1, \"mango<\/span>\")\nprint<\/span>(my_list<\/span>)  # this<\/span> will<\/span> print<\/span> ['apple'<\/span>, 'mango'<\/span>, 'banana'<\/span>, 'cherry'<\/span>, 'dragonfruit'<\/span>]<\/span>\n<\/code><\/span>Code language:<\/span> CSS<\/span> (<\/span>css<\/span>)<\/span><\/small><\/pre>\n\n\n

    Python also provides several methods to remove elements from a list. You can use the remove()<\/code> method to remove a specific item, or the pop()<\/code> method to remove an item at a specifiedindex.<\/p>\n\n\n

    my_list<\/span>.remove<\/span>(\"banana<\/span>\")\nprint<\/span>(my_list<\/span>)  # this<\/span> will<\/span> print<\/span> ['apple'<\/span>, 'mango'<\/span>, 'cherry'<\/span>, 'dragonfruit'<\/span>]<\/span>\nmy_list<\/span>.pop<\/span>(1)\nprint<\/span>(my_list<\/span>)  # this<\/span> will<\/span> print<\/span> ['apple'<\/span>, 'cherry'<\/span>, 'dragonfruit'<\/span>]<\/span>\n<\/code><\/span>Code language:<\/span> CSS<\/span> (<\/span>css<\/span>)<\/span><\/small><\/pre>\n\n\n

    Control Flow and Functions<\/h2>\n\n\n\n

    Python uses control flow tools such as if<\/code>, for<\/code>, and while<\/code> statements for handling conditions and looping through code.
    An if<\/code> statement is used to test a condition. If the condition is true, Python executes the block of code inside the if<\/code> statement:<\/p>\n\n\n

    x = 10<\/span>\nif<\/span> x > 5<\/span>:\n    print<\/span>(\"x is greater than 5\"<\/span>)  # this will be printed as x is indeed greater than 5<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    For<\/code> and while<\/code> loops are used to repeatedly execute a block of code. A for<\/code> loop iterates over items in a sequence such as a list, while a while<\/code> loop continues execution as long as a certain condition holds true:<\/p>\n\n\n

    for<\/span> i in range(5<\/span>):\n    print<\/span>(i)  # this will print numbers from 0 to 4<\/span>\nx = 5<\/span>\nwhile<\/span> x > 0<\/span>:\n    print<\/span>(x)  # this will print numbers from 5 to 1<\/span>\n    x -= 1<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    Functions in Python are blocks of reusable code that perform a specific task. You define a function using the def<\/code> keyword:<\/p>\n\n\n

    def greetings(name):\n    print<\/span>(\"Hello, \"<\/span> + name)\ngreetings(\"Alice\"<\/span>)  # this will print \"Hello, Alice\"<\/span>\n<\/code><\/span>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

    Conclusion<\/h2>\n\n\n\n

    Python is a flexible and powerful programming language that is widely used in various fields, from web development to data science. This guide provided an introduction to Python\u2019s basic concepts, including variables, operations, strings, lists, control flow, and functions. With a good understanding of these fundamentals, you\u2019ll be well-equipped to tackle more complex Python projects and further enhance your coding skills.<\/p>\n","protected":false},"excerpt":{"rendered":"

    This article is an introduction to Python for beginners, with the aim of equipping you with the basic knowledge and tools you need to explore and solve more complex problems. Understanding Variables and Operations In Python, a variable is a placeholder for storing a value. The value of a variable can be changed, and it … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"http:\/\/harvard-open-data-project.local\/wp-json\/wp\/v2\/pages\/19"}],"collection":[{"href":"http:\/\/harvard-open-data-project.local\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/harvard-open-data-project.local\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/harvard-open-data-project.local\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/harvard-open-data-project.local\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":1,"href":"http:\/\/harvard-open-data-project.local\/wp-json\/wp\/v2\/pages\/19\/revisions"}],"predecessor-version":[{"id":20,"href":"http:\/\/harvard-open-data-project.local\/wp-json\/wp\/v2\/pages\/19\/revisions\/20"}],"wp:attachment":[{"href":"http:\/\/harvard-open-data-project.local\/wp-json\/wp\/v2\/media?parent=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}