Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Python is one of the many languages used by the data science community to perform data manipulation, statistical modeling and machine learning. Its design philosophy emphasizes code readability. The python community is huge, offering an enormous library of technical support documentation. If you don't know how to do something in Python, chances are, someone else asked a similar question online and received a comprehensive answer.
This page provides syntax for different data types in Python as well as some of their associated functions. Each section includes an example to demonstrate the described syntax or function.
A string is a sequence of one or more characters (index values start at 0)
Action | Function |
---|---|
Input:
Output:
W3 Schools: Python Strings
get word length
len("abc")
extract nth character from word
"abc"[n]
extract substring nth-mth character from word
"abc"[n:m]
search for character in word
"abc".index("character")
search for subword in word
"ab" in "abc"
remove white spaces from the end of a word
"abc ".strip()
remove last character from word
"abc"[:-1]
determine data structure type
type("abc")
Instructions for installing Python on macOS and Windows operating systems can be found here.
For most users, it is recommended to download the current stable release from https://www.python.org/downloads/.
Some developers might wish to use a different version, or to switch between versions. For this, the Python version manager can be useful.
Python is also available for use in Brown's Computing Environments:
Oscar (for high-performance computing)
Stronghold (for secure computing)
The following instructions have been tested on computers running macOS 16 Big Ventura. In order to check the macOS version running on your computer, click on the "apple" icon in the top left hand corner of your screen and select "About This Mac." A window will pop up that includes a version number. Confirm you are running at least Version 16.X (where 'X' is any number). These instructions will likely work with earlier versions of macOS as well. If you are not running macOS 11.X Big Sur, you can upgrade for free following the instructions provided on Apple's website.
Download Python
Navigate to https://www.python.org/downloads/ and download the most recent version of Python for macOS.
Install Python
Open the downloaded file (e.g., python-3.12.3-macos11.pkg). A window will pop up with installation instructions. Progress through the prompts until Python has been installed in your Applications folder. Next, double click on the Python folder shortcut in your Applications folder to open it.
Run Python
Open, Terminal, type python3
, and hit return. Python should open. To quit Python, type quit()
and hit return.
Troubleshooting
If you get a Permission denied
error, rerun the command prepended with sudo
. You will be prompted to enter your computer password.
The following instructions have been tested on computers running Windows 10. Confirm that you are running at least Windows 10. These instructions will likely work with earlier versions of Windows, however they have not been tested.
Download Python
Navigate to https://www.python.org/downloads/ and download the most recent version of Python for Windows (32-bit or 64-bit depending on the specifications of your device).
Install Python
Open the downloaded file (e.g., python-3.10.10-amd64.exe). A window will pop up with installation instructions. Progress through the prompts until Python has been installed on your device. When prompted with Advanced Options, make sure to check "Add Python to environment variables".
Run Python
Open Command Prompt, type py
, and hit enter. Python should open to quit Python, type quit()
and hit return.
This is the typical first program for those new to a general purpose programming language like Python. It can be used to test that the Installation of Python is working and also introduce Python's basic syntax using the REPL environment or running code written using a Text Editor at the Unix command line.
Input:
Output:
Here are variations of the "Hello, World!" programming using variables and different print statements.
Input:
Output:
In order to assign variables in Python, you write the desired name for your variable, an “=” sign, and what the value of the variable should be.
Input:
Output:
We can write comments on our code, which do not run, to describe what certain lines of code or section of code do
These comments are just for the programmer, they will not appear anywhere in the output and just are there to explain what the code is doing or to provide helpful notes
To make a comment in Python, you can use the “#” symbol and then type your comment
Sometimes you might want to write longer comments that span multiple lines – to do this you can surround these comments with three tick marks above the start as well as three tick marks below the end
Input:
Output:
Without using a print statement, Python will only print out the most recent item that has an output. In order to print multiple things, we can use the print() function
Input:
Output:
Python is very sensitive with its indentation notation. Indentation should only be used in hierarchical structures, such as a class, function, or loop. Indents in improper locations will cause an error
Input:
Output:
Use Python in Brown Oscar Computing Environment - Forthcoming!
Use Python in Brown Stronghold Computing Environment - Forthcoming!
In computer programming, a package is a collection of modules or programs that are often published as tools for a range of common use cases, such as text processing and doing math. Programmers can install these packages and take advantage of their functionality within their own code.
This page provides instructions for installing, using, and troubleshooting packages in Python.
This page provides syntax for using numbers and mathematic operations in Python. Each section includes an example to demonstrate the described syntax and operations.
Integer (positive and negative counting number) - e.g., -3, -2, -1, 0, 1, 2, and 3:
int
- holds signed integers of non-limited length
long
- holds long integers (exists in Python 2.X, depreciated in Python 3.X)
Float (real or floating point numbers) - e.g., -2.14, 0.0, and 3.777
float
Boolean: (0 = False and 1 = True)
bool
Use type()
function to determine type
Input:
Output:
Input:
Output:
Input:
Output:
Create a Health Calculator Using Python - Forthcoming!
W3 Schools: Python Data Types
W3 Schools: Python Arithmetic Operators
W3 Schools: Python Numbers
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. [1]
This page provides syntax for some of the common control flow methods in Python. Each section includes an example to demonstrate the described methods
Test if a specified expression is true or false
Short-circuit evaluation
Test if all of the conditions are true x and y
Test if any of the conditions are true x or y
Test if a condition is not true not z
Conditional evaluation
if
statement
if-else
if-elif-else
Ternary operator
true_value if
condition else
false_value
Input:
Output:
Repeat a block of code a specified number of times or until some condition is met
while
loop
for
loop
Use break
to terminate loop
Input:
Output:
Input:
Output:
Python Documentation: Control Flow
Python Wiki: For Loops
W3 Schools: Python For Loops
W3 Schools: Python Conditionals and If Statements
Python comes with a full-featured interactive command-line REPL (read-eval-print loop) built into the
python
executable. In addition to allowing quick and easy evaluation of Python statements, it has a searchable history, tab-completion, many helpful keybindings, and dedicated help?
and shell modes;
.
This page provides examples of using REPL on the command line
Type python
in terminal to launch REPL
Type "help
" to enter help pages within REPL
Type a function from Python to read help pages (ex:print
)
Press q
to quit
Regular expressions are powerful tools for pattern matching and text processing. They are represented as a pattern that consists of a special set of characters to search for in a string
str
. The regex module needs to be imported before use.
This page provides syntax for regular expressions in Python . Each section includes an example to demonstrate the described methods.
Action | Function |
---|---|
Character class specifies a list of characters to match ([...]
where ...
represents the list) or not match ([^...]
)
Anchors are special characters that can be used to match a pattern at a specified position
Repetition or quantifier characters specify the number of times to match a particular character or set of characters
Input:
Output:
W3 Schools: Python RegEx
In computer programming, a collection is a grouping of some variable number of data items (possibly zero) that have some shared significance to the problem being solved and need to be operated upon together in some controlled fashion. []
This page provides syntax for different types of collections and data structures in Python (arrays, sets, dictionaries, etc.). Each section includes an example to demonstrate the described methods
Arrays are ordered collections of elements. In Python they are automatically indexed (consecutively numbered) by an integer starting with 0.
Action | Syntax |
---|
Action | Syntax |
---|
Action | Syntax |
---|
Input:
Output:
Sets are an unordered collection of unique elements.
Input:
Output:
Dictionaries are unordered collection of key-value pairs where the key serves as the index (“associative collection”). Similar to elements of a set, keys are always unique.
Input:
Output:
Many Python programs involve the input and output of files. When analyzing a dataset, that dataset file will need to be pulled into your program (input). If you want to see the results of your analysis, your program will need an output.
This section provides the syntax for inputting files (reading) and outputting results (writing) using base Python (i.e, no packages such as Pandas)
Tabulate and report counts for sex in from the .
Dataset (example lines from adult.data
)
Input (process_file.py
)
Output
Terminal
Analyze the MIMIC-IV Demo Files Using Julia - Forthcoming!
Analyze the SyntheticRI Demo Files Using Julia - Forthcoming
Operator | Example |
---|---|
Operator | Example |
---|---|
Operator | Example |
---|---|
Anchor | Special Character |
---|---|
Repetition | Character |
---|---|
Action | Syntax |
---|
Action | Syntax |
---|
Action | Syntax |
---|
Action | Syntax |
---|
Action | Syntax |
---|
Action | Syntax |
---|
Action | Syntax |
---|
Action | Syntax |
---|
Action | Syntax |
---|
Wikipedia contributors (n.d.). Collection. In Wikipedia. Retrieved May 1, 2024, from
W3 Schools:
Data Quest:
Tutorials Point:
Data Science Central:
Addition
x + y
Subtraction
x - y
Multiplication
x * y
Division
x / y
Floor Division
x//y
Power (Exponent)
x ** y
Remainder (Modulo)
x % y
Equality
x == y or isequal(x, y)
Inequality
x != y or !isequal (x, y)
Less than
x < y
Less than or equal to
x <= y
Greater than
x > y
Greater than or equal to
x >= y
Check if regex matches a string
re.search("pattern", string, flag=0)
Capture regex matches
re.match("pattern", string, flag=0)
Specify alternative regex
pattern1|pattern2
Character Class
...
Any lowercase vowel
[aeiou]
Any digit
[0-9]
Any lowercase letter
[a-z]
Any uppercase letter
[A-Z]
Any digit, lowercase letter, or uppercase letter
[a-zA-Z0-9]
Anything except a lowercase vowel
[^aeiou]
Anything except a digit
[^0-9]
Anything except a space
[^ ]
Any character
.
Any word character (equivalent to [a-zA-Z0-9_]
)
\w
Any non-word character (equivalent to [^a-zA-Z0-9_]
)
W
A digit character (equivalent to [0-9]
)
\d
Any non-digit character (equivalent to [^0-9]
)
\D
Any whitespace character (equivalent to [\t\r\n\f]
)
\s
Any non-whitespace character (equivalent to [^\t\r\n\f]
)
\S
Beginning of line
^
End of line
$
Beginning of string
\A
End of string
\Z
Zero or more times
*
One or more times
+
Zero or one time
?
Exactly n times
{n}
n or more times
{n,}
m or less times
{,m}
At least n and at most m times
{n.m}
Equality
x == y
Inequality
x != y
Less than
x < y
Less than or equal to
x <= y
Greater than
x > y
Greater than or equal to
x >= y
Add element to end |
|
Remove element from end |
|
Remove element from beginning |
|
Add element to beginning |
|
Sort array (will not change array itself) |
|
Sort array in place (will change array) |
|
Get unique elements in array |
|
Intersection |
|
Union |
|
New set (empty) |
|
Set with values |
|
Set with values |
|
Get length of set my_set |
|
Check if value is in set |
|
Add value |
|
Intersection |
|
Union |
|
Difference |
|
New Dictionary (empty) |
|
Dictionary with values |
|
Get value for key in dictionary |
|
Check if dictionary has key |
|
Check for key/value pair |
|
Get value and set default |
|
Add key/value pair |
|
Delete key/value pair |
|
Get keys |
|
Get values |
|
Convert keys to array |
|
Convert values to array |
|
Sorting keys |
|
Sorting values |
|
Sort by value (descending) with keys |
|
Sort by value (ascending) with keys |
|
Get top n by value (e.g., 3) |
|
New array (empty) |
|
Array with values (integers) |
|
Array with values (string) |
|
Array of numbers |
|
Split string str by delimiter into words (e.g., space) |
|
Get length of array |
|
Get first element of array |
|
Get last element of array |
|
Get nth element of array |
|
Check if element is in array |
|