(U)Unclassified

Python Features

Python Features Overview

  • The Language
  • Python Usage
  • PyDocs
  • Standard Library
  • Styling and PEP8
  • Objects
  • 2.x and 3.x differences
  • Python Interpetor
  • Running Python

Objectives

Python Features Objectives

Introduction to Python

guido
  • Python is a high level programming language used for generic-purpose programming
  • Python was created by Gudio van Rossum and released in 1991
  • It's dynamic type system, auto memory management and standard library
  • Open Source

Why Python?

  • High Level: strong abstraction of details from computer
  • Interpreted: directly executes instructions and provides immediate feedback
  • Readable: very easy to read, learn and understand
  • Cross-Platform: runs almost anywhere

Why Python (Cont.)

  • Batteries-Included: deep functionality built into standard library
  • General Language: can be used to do just about anything.
  • Object Oriented Programming: allows code to be reused, encapsulated, maintained easier and organized cleaner.

Python Usage

  • Prototyping
  • Cross-platform scripts
  • Automation and testing
  • Vulnerability Research/Fuzzing
  • Create/replicate pieces of software you do not otherwise have access to

Python Usage

  • Web Development
  • GUI/UX
  • Gaming
  • Etc.

PyDoc(s)

  • Python Documentation and is the go-to resource for everything Python.
  • PyDoc is a command that evokes the Python interpreter and utilizes the help() function
  • You can also open up the Python interpreter and use help(object) replacing object with the object you want to look up.
  • help() is a function that gives you a TON of data. For instance, object methods, module, important information, help, etc.
  • Understanding how to use PyDocs, pydoc command and help() function is not only testable... but it's an essential piece in becoming proficient in Python.

2.7 Docs

3.x Docs

Standard Library

Styling and PEP8

  • Python Enhancement Protocol 8
  • Python is whitespace sensitive!
    • Python does not use brackets
    • Python utilizes an indentation system.
  • Spaces are preferred over tabs, you cannot mix.
    • PEP8 commands 4 spaces per indention level (tab)

PEP8

Documenting Code

Python brings many features we are already familiar with in other programming languages, but gives the user a bit more power in their documentation.

							
								# This is a comment
							
						
							
								"""This is a single line docstring"""
							
						
							
								"""This is a multiline 
								comment"""

							
						
							
								"""This is a multiline docstring

								Notice the separation? Let's get into that and explain it. 
								"""
							
						

What is the difference between Docstrings and Comments?

Docstrings are used to provide a convenient way to associate modules, functions, classes and methods with Python documentation.

  • Docstrings are best used to describe what a section of code does.
  • Comments focus on the how and implementation of the code.
							
								"""This function takes a list of strings and returns a deduplicated copy."""

								# convert the list to a set then back to a list to dedup.
							
						

Exercise

  • Take a moment to step through PEP8 and and learn the styling and formatting requirments
  • Pay most attention to the basics:
    • Indentation
    • Maximum line length
    • The entire comments section
      • Ensure to include a docstring at the top of all of your labs that includes your:
        • name
        • project/lab
        • date
    • Naming Conventions
    • Remember what else in contained within and return to PEP8 as we learn new concepts. You are expected to abide by PEP8 during the class.

Objects in Python

  • Everything in Python is an Object
    • An object is just a struct
    • An encapsulation of variables and functions into a single entity
    • Inheritance simply means an object can be assigned to a variable or passed into a function for example

Objects Lab

Lab 1A

Py2 vs Py3 Differences

Python 2 and 3 are similar but NOT COMPATIBLE! Python 3 does break compatibility. We will be focusing on Python 3.7 but will cover 2.7 along side the entire course. Here are just a few of the major differences:

Print

Python 3 replaced Python 2's print statement with a print function(). While Python 2 will be happy as is or with parentheses... Python 3 will throw an error if the parentheses are not included.

Python 2 Print

							
								print 'Hello World!' # outputs: Hello World!
								mystr = 'Goodbye World!' # Notice we didn't declare a type?
	
								print mystr # outputs: Goodbye World!
								print("This works too") # This works too!
							
						

Python 3 Print

							
								print("Hello World!") # Hello World!
								mystr = 'Goodbye World!' # Again, no type declared. It's automatically determined. 
	
								print(mystr) # Goodbye World!
							
						

Integer Division

  • Python 2 treats numbers without a decimal as integers
  • Python 3 will treat them as float if it applies
  • Python 2 Division with integers truncates the remainder! Setting one number as a real number will yield the correct result

Python 2

							
								print 3/2 # 1
								print 3.0/2 # 1.5
							
						

Python 3

							
								print (3/2) # 1.5
							
						

Other Major Differences

Unicode

  • Python 2 has ASCII str() type, separate unicode() type.. but no byte() type
  • Python 3 has Unicode (utf-8) str-ings and two byte classes: byte and bytearray

input() vs raw_input()

  • Python 3 input() always stores userinput as str objects
  • Python 2 input() on the other hand fully evaluates the code the user gives it
    • This is dangerous, and can be used in a malicious way
    • Instead, we use raw_input() in Python 2... which converts user input to a string.

Running Python

Python Interpreter

The Python Interpreter uses a concept called REPL:

  • Read the user input
    • Some constructs like loops might be multiple lines
  • Evaluate the input
    • Attempt to perform the instruction
  • Print: to the screen and
    • Print any requested info or an error with stack trace
  • Loop:
    • Print the next user prompt or loop until evalution complete

Launching Python

The Python Interpreter can be launched from the command prompt or terminal using the following commands:

Linux and OS X

  • python2 or python generally opens up Python 2
  • python3 generally opens up Python 3 (will open an out dated Python 3 one some distros)
  • python3.7 opens up Python 3.7 currently on Fedora
  • This is all dependent on a number of factors
  • You can create aliases if you wish

Windows

  • py -2 generally opens up Python 2
  • py -3 or py generally opens up Python 3
  • Again, this is all dependent on a number of factors

Additional Commands/Info

  • exit() or shorcut ctrl-D to exit the Python Interpreter
  • Typing simply a variable or expression will yield a printed output
							
								# >>> represents the interpreter asking for a command. 

								>>> x = 100
								>>> x
								100
								>>> x + 5
								105
								>>> x
								100
							
						

Conditionals can be checked and loops can be executed. Be sure to provide indentation. This can be done via pressing the space bar 2 or 4 times per line or pressing tab.

Python Source Files

The other way to run Python code is using source files with the extension of .py. Python does not require compilation on the user's end. Executing .py source code is similar to starting the Interpreter.

Be sure to:

  • Use appropiate bash commands for the source code's Python version
  • Include filename and extension after bash command
  • Include any arguments after filename and extension.
  • We will go over ways to execute multiple files later in the course.

Executing files

							
								$ ls
								file.txt    fileIO.py    hello_world.py    hello_world_3.py

								$ python hello_world.py
								Hello World!

								$ python3 hello_world_3.py
								Hello World for Python 3!
							
						

Questions?

Summary

  • The Language
  • Python Usage
  • PyDocs
  • Standard Library
  • Styling and PEP8
  • Objects
  • 2.x and 3.x differences
  • Python Interpetor
  • Running Python