<jupyter_start><jupyter_text>Introduction to Python  Properties of PythonPython is **strongly typed** (i.e. types are enforced), **dynamically, implicitly typed** (i.e. you don’t have to declare variables), **case sensitive** (i.e. var and VAR are two different variables) and **object-oriented** (i.e. everything is an object).  Installation* Go to https://github.com/psychopy/psychopy/releases and download the latest release. For this course we will use the python 3 release, but the 2 release should work as well (with minor modifications to code)* Install Psychopy and open it when it is done installing* If the builder view opens, click view in the menu and "Go to Coder View"* At the bottom of the screen, you'll see a shell tab, click it and we'll continue<jupyter_code># To get help, use help(<object>), dir(<object>), and <object>.__doc__
# help(<object>) shows you how the object works
# dir(<object>) gives you all of the object's methods
# <object>.__doc__ shows you the documentation string for the object
import numpy as np
help(np.random.sample)
dir(np.random)
print(np.random.__doc__)
# Variables
foo = 3
print(foo)
foo += 2
print(foo)
foo -= 1
print(foo)
"""A multiline comment of docstring.
You can write whatever you want in here"""
foo = "Hello"
foo += " world."
print(foo)
# Data Types
# Lists
random_list = ["A List", ["Can contain"], ["Many types of data"], 1, 2, 1.5]
print(random_list)
# Accessing elements
# Lists are 0 indexed, meaning 0 is the first item
print(random_list[0])
# Can access elements starting at the end
print(random_list[-1])
# Dictionaries
mydict = {"Name": "James", "Age": 55, "pi": 3.1415}
print(mydict["Name"])
# Change the name
mydict["Name"] = "Steve"
print(mydict["Name"])
# Tuples are just lists that are immutable
# (You can't change tuples after they're created)
mytuple = (1, 2, 3)
print(mytuple[0])
print(len(mytuple))
# Ranges and slicing
mylist = [0, 1,2,3,4,5,6,7,8,9]
print(mylist[1:])
print(mylist[-3:-1])
print(mylist[1:])
print(mylist[::2]) # Step parameter
# Strings
print("This is a string")
print("My name is {}\n".format('Aaron'))

longStr = """This is a string
that spans
multiple lines"""
print(longStr)
# Flow control (program logic)
# if, for, while
for i in range(10):
    print(i)

if 1 is not None:
    print("1 is not None")
if 1 == 1:
    print("1 is equal to 1")

x = 0
while x < 10:
    print("x is {}".format(x))
    x += 1
# Functions

def fib(n):
    """Get the nth number in a fibonacci sequence."""
    if n == 0:
        return 0
    if n == 1:
        return 1
    return fib(n-1) + fib(n-2)

for i in range(10):
    print(fib(i))

# Anonymous functions
# Sometimes you don't need to define an entire function, so you create an anonymous one.
# You can do the same in Matlab with @(x) x.^2
sqr = lambda x: x**2
print(sqr(2))
# Classes and object oriented programming
class ClassA(object):
    def __init__(self):
        self.foo = 3
    def print_foo(self):
        print(self.foo)
    
instance = ClassA()
instance.print_foo()
instance.foo = 5
instance.print_foo()
# Exceptions
def invalid_function():
    try:
        10 / 0
    except ZeroDivisionError:
        print("Not a valid function")
    else:
        # No exception occurred.
        pass
    finally:
        # This is executed regardless of whether an exception
        # happened or not
        print("This function is finally over")
        
invalid_function()
# Using external libraries (importing libraries)
import random

print(random.randint(1,100))

from random import randint
print(randint(1,100))<jupyter_output>5
34
{'fraction_non_alphanumeric': 0.12416107382550336, 'fraction_numerical': 0.02181208053691275, 'mean_word_length': 3.9134615384615383, 'pattern_counts': {'":': 3, '<': 11, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '354115', 'license': 'MIT', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1143, 'n_words': 452, 'path': 'Class1.ipynb', 'repo_name': 'arkravitz/psychopy-course', 'size': '3576'}