FORMULA ONE


Welcome to FORMULA ONE!!! We are a team of young, passionate students who have begun our journey at Carleton very recently and are now looking for ways to give back to the Carleton community by doing the needful for the upcoming First Years. From such a thought, Formula One has taken its beginning.


WHAT IS IT?

A guide for the first-year students at the Carleton department of Bachelor of Computer Science. It highlights all of the necessary skills and tools to be aware of as an incoming student and most importantly, it is an opportunity for such students to be exposed to similar material namely COMP 1405 and COMP 1406 to that of the course materials. (NOTE: This is not a replacement for any of the course materials/lectures; it is simply a way to get familiarized with the programming skill beforehand, to avoid a blank state of mind during class.


COMP 1405

COMP 1405: Introduction to Computer Science I

A first course in programming for B.C.S. students emphasizing problem solving and computational thinking. Topics include pseudocode, variables, conditionals, iteration, arrays, objects, functions, sorting, searching, and simulation.

Includes: Experiential Learning ActivityPrecludes additional credit for COMP 1005ECOR 1041ECOR 1042ECOR 1051ECOR 1606SYSC 1005, SYSC 1100 (no longer offered), CGSC 1005BIT 1400.Prerequisite(s): restricted to students registered in the B.C.S. program, combined Honours in Computer Science and Mathematics, Honours Computer Mathematics, and Honours Computer Statistics.Lectures three hours a week, tutorial one and a half hours a week.


Introduction

Python is a high level general purpose programming language. Its programming abilities enables the programmers to code in a simple way which can be comprehended easily. It supports procedural and object oriented programming.

Python was invented by Guido van Rossum in 1980's. The first version of python i.e. version 0.9.0 was released in the year 2000 with a lot of new features.


Python Setup

Most of the computers sold today have a pre-installed version of python. But as the Carleton computer science community, we installed the software named as WING 101.

WING 101 INSTALLATION PROCESS:

  1. The most recent software can be found on the webpage linked here https://www.python.org/downloads/.
  • For windows operating systems, download the Windows x86-64 executable installer.

  • For macOS or any newer release, there is only one installer available on the website.

    1. After clicking the website for downloading the python software, click to go to the page of downloads.

    2. Use the newest version of the software that is available on the webpage.

    3. Click on the newest version, and scroll down to the bottom of the page (where there a list of links you can download from).

    4. Download Windows x80-64 executable installer and for mac users, use the appropriate mac installers.

    5. Double click on the downloaded file from the file manager.

    6. Once the application has opened, DONT FORGET TO ADD PATH TO THE INSTALLATION PROCESS, and then click install.


Python Syntax

Python is a simple and a great back end software that can be used for developing software's and other simple programs. For every programming software, there is a particular syntax to follow. you don't need to worry because it is pretty simple. Follow the hierarchy of dropdown boxes to learn this topic.

Python Indentation

This process for python is a very important step. This refers to the spaces that is left before a particular line of code. This may be used to improve readability in other programs but it is a crucial process in python coding.

  • Wing 101 enables automatic indentation

Correct piece of code:

if 1< 2:
	print("One is smaller than Two!")

Wrong piece of code:

if 1< 2:
print("One is smaller than Two!")

Comments

We can make comments in a certain python program to make it easier for the programmer and the reader. We can use the symbol # and make comments.

#Introduction to Python
x = 105

We can also make multiline comments by using triple double quotes.

"""
This way we can create multiline
comments in a particular python 
program.
"""

Strings

Strings are words/letters that are written using single, double quotation marks.

The following code assigns a string to a variable, which then can be used to manipulate.

example_string = "Hello, World!"

Access

example_string [1] # prints the second element in the string, in this case, 'e'
example_string [2:5] # range
example_string [5:] # slice from 6th to end

Modify

returns string in all uppercase

example_string.upper ()

returns string in all lowercase

example_string.lower ()

removes whitespaces from beginning or at the end

example_string.strip ()

repalces the argument 1 with argument 2 in the original string

example_string.replace ("H", "j")

returns a list with the words being being split at the given argument

example_string.split (",")

An example of multiple commands in one is as follows.

example_string.strip(string.punctuation).lower().split()

Output:

['hello', ' world!']

Lists

Lists are ordered collections with possible duplicates stored in square brackets " [ ] "

The following code assigns a list to a variable, which will then be used to manipulate.

example_list = ['a', 1, True, 4, 6, 'b']

Access

example_list [-1] # Backwards accessing
example_list [2:5] # Third, Fourth and Fifth item 
example_list [2:] # Access from third time to the end of the list

Change

example_list [0] = 'c' # It is not 'a' anymore, it is 'c' for the first element
example_list [1:3] = ['x', 3] # The second and third item are now 'x' and 3
example_list [1:3] = ['r'] # Replaces the range of items with just one item 'r'

Add/Extend

Without replacing, 'b' slides in between the the second and third item

example_list.insert (2, 'b')

Adds 'q' to the end of the list

example_list.append ('q')

Extend -> extend () works for any iterable objects e.g. tuples, sets, dictionaries, etc.

example_list2 = ['l', 10]

Adds the entire example_list2 list to the end of the list as elements

example_list.extend (example_list2)

Remove

example_list.remove ('a') # Removes the specific item
`example_list.pop(1) # Removes the specific item at that index
`del example_list[2] # Removes the specific item at that index
`example_list.clear() # Keeps the list empty by removing all elements

Looping

# For-Loop for each item
for i in range (len(example_list)):
     item = example_list[1]

# While Looping
i = 0
while i < len(example_list):
     item = example_list[i]
     i += 1

Tuples

A tuple is an ordered collection that is unchangeable and is stored in round brackets " ( ) "

The following code assigns a tuple to a variable, which will then be used to manipulate.

example_tuple = (1, 'a', True)

Access

example_tuple [1] # access

Update

tuple_list = list(example_tuple)
tuple_list [1] = 'b'
example_tuple = tuple(tuple_list)

Unpack

(x, y, z) = example_tuple

Sets

Sets are unordered collection and unindexed (cannot be accessed directly) stored in curly brackets "{ }"

The following code assigns a sample set to a variable, which can then be used to manipulate.

example_set = {1, 'a', True}

Access

for x in example_set:
     item = x

As part of access, you can check if a certain item is in the set, as shown below.

Check if in list -> Returns True or False

check = 1 in example_set

Add

Add Item

example_set.add ('b')

Add two sets ~ update() can be used to any iterable objects

example_set2 = {2, 'c', False}
example_set.update (example_set2)

Remove

example_set.remove ('a')

If 'a' does not exist, then discard() does not raise an error

example_set.discard ('a')

Dictionaries

Dictionaries are arranged in pairs of key:value with no duplicates and can be modified.

The code below assigns a dictionary to a variable which then can be called and manipulated.

example_dict = {"one": 1, "two": False, "three": 3, "four": 4}

Access

There are multiple ways to perform this action, as shown below.

example_dict ["one"] # Gives the value of the key "one"
example_dict.get ("one") # Gives the value of the key "one"
example_dict.keys () # Gives a list of keys
example_dict.values () # Gives a list of values
example_dict.items() # Gives a list of items of (keys, values) as tuples

Change

There are multiple ways to perform this action, as shown below.

example_dict ["two"] = True # Changes the value of key "two" to True
example_dict.update ({"four": False}) # Changes the item with the key "four"

Remove

There are multiple ways to perform this action, as shown below.

example_dict ["five"] = 5 # Adds a new item of "five":5 to the dictionary
example_dict.update ({"six": 6}) # Adds the item to the dictionary

Looping

This is used to access every single item or value/key, depending on the iteration loop.

for x in example_dict.keys (): # For all keys in the dictionary
     result = x
for y in example_dict.values (): # For all values in the dictionary
     result = x
for x, y in example_dict.items (): # For all items in the dictionary split into x, y
     result = (x, y)

Files

The following code shows how to read, modify and close the files with their particular order.

Always open the file with read, append or write, modify, then close the file.

example_file = "file-name.txt"

Opens file and "r" read (error if does not exist), "a" append and "w" write

infile = open(example_file, "r")

Returns one line, if called twice, it reads first two lines of the file

infile.readline() 
infile.close() # closes the file

Python Variables

In python, defining variables is just assigning a value to it. We don't have to declare the variable separately and define it's values to it.

a = 2
b = "Hello world!"

Variable Names

A certain variable name can be named as a and b or even given a more descriptive name like student_no, name, course etc.

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

Valid names

myvar = "Joe"
my_var = "Joe"   #myvar and my_var are two different variables

Invalid names

5var = 102
var 1 = 445

Assign multiple values to a variable

We can assign multiple values in python.

x, y, z = "A", "B", "C"
print(x)
print(y)
print(z)

Output

A
B
C

Python's Data Types

In python, we have different data types that we use in building our programs.

(Check the dropdown boxes for more information)

Text based variables

str

Numeric variables

int, float

Sequence based

list, tuple, range

Boolean values

Bool

More on Python numbers

INT

a = 5 
b = 7555684
c = -63348
print(type(a))     # These statements will give the variable type 
print(type(b))
print(type(c))

Output

<class 'int'> <class 'int'> <class 'int'>


FLOAT

a = 5.33
b = 755.5684
c = -6334.8
print(type(a)) # These statements will give the variable type
print(type(b))
print(type(c))

Output

<class 'float'>
<class 'float'>
<class 'float'>

Type conversion

a = int(11) # a will be 11
b = int(2.812) # b will be 2
c = int("34") # c will be 34

Output

11
2
34
f = float(8.5) # f will be 8.5
d = float("7") # d will be 7.0

Output

8.5
7.0
k = str("string1") # k will be 'string1'
o = str(452) # o will be '2'

Output

'string1'
'2'

Boolean values

Boolean values gives you a True or False output based on the

statement that is provided to the compiler.

print(175 > 984)
print(10 == 74)
print(1 < 9)

Output

False
False
True

COMP 1406

Syntax

Process

Comments

Variables and Data Types

Operators

Strings

Formatting Texts

Control Structure: Loops and Branches