{ "cells": [ { "cell_type": "markdown", "id": "e49c42fb", "metadata": {}, "source": [ "# Python Basics \n", "\n", "the following cells have been obtained from the python introduction notebook of\n", "Jeffrey Kantor. These examples and urther can be fund under https://github.com/jckantor/CBE30338\n", "\n", "## variables and primitive data types" ] }, { "cell_type": "code", "execution_count": 1, "id": "f441c06d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n", "3.1415\n" ] } ], "source": [ "#A variable stores a piece of data and gives it a name\n", "answer = 42\n", "\n", "#answer contained an integer because we gave it an integer!\n", "\n", "is_it_thursday = True\n", "is_it_wednesday = False\n", "\n", "#these both are 'booleans' or true/false values\n", "\n", "pi_approx = 3.1415\n", "\n", "#This will be a floating point number, or a number containing digits after the decimal point\n", "\n", "my_name = \"Jacob\"\n", "#This is a string datatype, the name coming from a string of characters\n", "\n", "#Data doesn't have to be a singular unit\n", "\n", "#p.s., we can print all of these with a print command. For Example:\n", "print(answer)\n", "print(pi_approx)" ] }, { "cell_type": "markdown", "id": "3f1994d1", "metadata": {}, "source": [ "# List and Dictionaries" ] }, { "cell_type": "code", "execution_count": 2, "id": "0d8ff40a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Green', 'Blue', 'Red']\n", "[10, 20, 30, 40, 50, 'Sixty']\n", "Green\n", "Red\n", "Rosa Parks\n" ] } ], "source": [ "#What if we want to store many integers? We need a list!\n", "prices = [10, 20, 30, 40, 50]\n", "\n", "#This is a way to define a list in place. We can also make an empty list and add to it.\n", "colors = []\n", "\n", "colors.append(\"Green\")\n", "colors.append(\"Blue\")\n", "colors.append(\"Red\")\n", "\n", "print(colors)\n", "\n", "#We can also add unlike data to a list\n", "prices.append(\"Sixty\")\n", "\n", "#As an exercise, look up lists in python and find out how to add in the middle of a list!\n", "\n", "print(prices)\n", "#We can access a specific element of a list too:\n", "\n", "print(colors[0])\n", "print(colors[2])\n", "\n", "#Notice here how the first element of the list is index 0, not 1! \n", "#Languages like MATLAB are 1 indexed, be careful!\n", "\n", "#In addition to lists, there are tuples\n", "#Tuples behave very similarly to lists except that you can't change them \n", "# after you make them\n", "\n", "#An empty Tuple isn't very useful:\n", "empty_tuple = ()\n", "\n", "#Nor is a tuple with just one value:\n", "one_tuple = (\"first\",)\n", "\n", "#But tuples with many values are useful:\n", "rosa_parks_info = (\"Rosa\", \"Parks\", 1913, \"February\", 4)\n", "\n", "#You can access tuples just like lists\n", "print(rosa_parks_info[0] + \" \" + rosa_parks_info[1])\n", "\n", "# You cannot modify existing tuples, but you can make new tuples that extend \n", "# the information.\n", "# I expect Tuples to come up less than lists. So we'll just leave it at that. \n" ] }, { "cell_type": "markdown", "id": "586e8794", "metadata": {}, "source": [ "# Conditions, Logical operators and If-Statements\n", "The word “if” is a keyword. When Python sees an if-statement, it will determine if the associated logical expression is true. If it is true, then the code in code block will be executed. If it is false, then the code in the if-statement will not be executed. The way to read this is “If logical expression is true then do code block.”\n", "\n", "When there are several conditions to consider you can include elif-statements; if you want a condition that covers any other case, then you can use an else statement." ] }, { "cell_type": "code", "execution_count": 3, "id": "8cf4c578", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Five is greater than two!\n", "One is equal to one\n", "5 does not equal 2 and 5 is greater than two!\n", "Five is NOT smaller than two!\n" ] } ], "source": [ "# To apply conditions in python the if statement is used.\n", "# The statement is based on boolean functions yielding either true or false\n", "#, these includes the following examples logical expressions.\n", "# == (equal), != (does not equal), < (smaller than), <= (equal or smaller than)\n", "# Individual expressions can be combined utilizing logical operators including\n", "#, e.g. \"and\" / \"or\" a more detailed description can found under https://pythonnumericalmethods.berkeley.edu/notebooks/chapter01.05-Logial-Expressions-and-Operators.html\n", "\n", "if 5 > 2:\n", " print(\"Five is greater than two!\") # please note that one as to indent the code block after a condition (4 spaces or 1 tab)\n", "\n", "if 1 == 1:\n", " print(\"One is equal to one\")\n", "\n", "if 5 != 2 and 5 > 2:\n", " print(\"5 does not equal 2 and 5 is greater than two!\" )\n", "\n", "if 5 < 2:\n", " print(\"Five is smaller than two!\")\n", "else:\n", " print(\"Five is NOT smaller than two!\")" ] }, { "cell_type": "markdown", "id": "c202d6f6", "metadata": {}, "source": [ "# Getting started with GeoData\n", "\n", "the following part has been obtained from the official geopandas introduction notebook.\n", "You can find the original and further examples under this link https://geopandas.org/en/stable/getting_started/introduction.html\n" ] }, { "cell_type": "markdown", "id": "8f49f924", "metadata": { "tags": [] }, "source": [ "# Introduction to GeoPandas\n", "\n", "This quick tutorial introduces the key concepts and basic features of GeoPandas to help you get started with your projects.\n", "\n", "## Concepts\n", "\n", "GeoPandas, as the name suggests, extends the popular data science library [pandas](https://pandas.pydata.org) by adding support for geospatial data. If you are not familiar with `pandas`, we recommend taking a quick look at its [Getting started documentation](https://pandas.pydata.org/docs/getting_started/index.html#getting-started) before proceeding.\n", "\n", "The core data structure in GeoPandas is the `geopandas.GeoDataFrame`, a subclass of `pandas.DataFrame`, that can store geometry columns and perform spatial operations. The `geopandas.GeoSeries`, a subclass of `pandas.Series`, handles the geometries. Therefore, your `GeoDataFrame` is a combination of `pandas.Series`, with traditional data (numerical, boolean, text etc.), and `geopandas.GeoSeries`, with geometries (points, polygons etc.). You can have as many columns with geometries as you wish; there's no limit typical for desktop GIS software.\n", "\n", "![geodataframe schema](../_static/dataframe.svg)\n", "\n", "Each `GeoSeries` can contain any geometry type (you can even mix them within a single array) and has a `GeoSeries.crs` attribute, which stores information about the projection (CRS stands for Coordinate Reference System). Therefore, each `GeoSeries` in a `GeoDataFrame` can be in a different projection, allowing you to have, for example, multiple versions (different projections) of the same geometry.\n", "\n", "Only one `GeoSeries` in a `GeoDataFrame` is considered the _active_ geometry, which means that all geometric operations applied to a `GeoDataFrame` operate on this _active_ column.\n", "\n", "\n", "