Intro to Python

Lecture 2

Dr. Greg Chism

University of Arizona
INFO 511 - Fall 2024

Intro to Python

What is Python?

  • “Python is the second best language at everything.” - Van Lindberg

  • Versatile and popular programming language with simple syntax

  • Large collection of frameworks and libraries

  • Large, active community

  • Widely used for web development, data analysis, artificial intelligence, scientific computing, and more.

Basic syntax and comments

# This is a comment
print("Hello, Python!")  # This prints a message
Hello, Python!
  • Indentation for code blocks (instead of brackets)

  • Comments start with a # (used to explain code)

Variables and data types

# Integer
x = 5

# Float
y = 3.14

# String
name = "Python"

# Boolean
is_easy = True
  • Variables store data values.

  • Python uses integers (whole numbers), floats (non-whole numbers), strings (text), and booleans (true/false).

Factors (categorical data)

import pandas as pd

x = pd.Categorical(["a", "b", "b", "a"])
print(x)
print(type(x))
print(x.categories)
print(x.codes)
['a', 'b', 'b', 'a']
Categories (2, object): ['a', 'b']
<class 'pandas.core.arrays.categorical.Categorical'>
Index(['a', 'b'], dtype='object')
[0 1 1 0]

Other classes

Date

import datetime

today = datetime.date.today()
print(today)
print(type(today))
print(dir(today))
2024-08-19
<class 'datetime.date'>
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'ctime', 'day', 'fromisocalendar', 'fromisoformat', 'fromordinal', 'fromtimestamp', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'min', 'month', 'replace', 'resolution', 'strftime', 'timetuple', 'today', 'toordinal', 'weekday', 'year']

Date-Time

now = pd.Timestamp("2024-02-08 11:45:00", tz="EST")
print(now)
print(type(now))
print(now.tz)
2024-02-08 11:45:00-05:00
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
EST

Basic operations

# Arithmetic Operations
a = 10
b = 3
sum = a + b
difference = a - b
product = a * b
quotient = a / b

# Logical Operations
is_greater = a > b
is_equal = (a == b)
  • Python supports various arithmetic and logical operations.

Control structures: if-else

age = 20
if age >= 18:
    print("Adult")
else:
    print("Minor")
Adult
  • Python uses if, elif, and else for decision-making.

Loops

# For Loop
for i in range(5):
    print(i)

# While Loop
j = 0
while j < 5:
    print(j)
    j += 1
  • Python has two types of loops: for and while.

Lists

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Accessing the first item
  • Lists store multiple items in a single variable.
  • Access elements using index (starting at 0).

Functions

def greet(name):
    return "Hello " + name

print(greet("Alice"))
  • Functions perform specific tasks.

  • Call a function with its name and arguments.

Conclusion

  • Python is a versatile and user-friendly language.

  • Ideal for beginners and widely used.

  • Encourages readable and maintainable code.

  • Extensive libraries and community support.

Toolkit: Version control and collaboration

Git and GitHub

Git logo

  • Git is a version control system – like “Track Changes” features from Microsoft Word, on steroids
  • It’s not the only version control system, but it’s a very popular one

GitHub logo

  • GitHub is the home for your Git-based projects on the internet – like DropBox but much, much better

  • We will use GitHub as a platform for web hosting and collaboration (and as our course management system!)

Versioning - done badly

Versioning - done better

Versioning - done even better

with human readable messages

How will we use Git and GitHub?

How will we use Git and GitHub?

How will we use Git and GitHub?

How will we use Git and GitHub?

Git and GitHub tips

  • There are millions of git commands – ok, that’s an exaggeration, but there are a lot of them – and very few people know them all. 99% of the time you will use git to add, commit, push, and pull.
  • We will be doing Git things and interfacing with GitHub through VS Code, but if you google for help you might come across methods for doing these things in the command line – skip that and move on to the next resource unless you feel comfortable trying it out.
  • There is a great resource for working with git and Python: git-github-python. Some of the content in there is beyond the scope of this course, but it’s a good place to look for help.

Tour: Git + GitHub

Just one option for now:

Sit back and enjoy the show!