The Basics For Python

  • categories: [week1]

Basic vocabulary of Version Control

Few basic terms that are used often when discussing about version control (not exhaustive).

  • Repository = a location where all the files for a particular project are stored, usually abbreviated to “repo.” Each project will have its own repo, which is usually located on a server and can be accessed by a unique URL (a link to GitHub page for example).

  • Commit = To commit is to write or merge the changes made in the working copy back to the repository. Whe you commit, you are basically taking a “snapshot” of your repository at that point in time, giving you a checkpoint to which you can reevaluate or restore your project to any previous state. The terms ‘commit’ or ‘checkin’ can also be used as nouns to describe the new revision that is created as a result of committing.

  • Revision / version = A revision or a version is any change in made in any form to a document(s). Clone = Cloning means creating a repository containing the revisions from another repository. This is equivalent to pushing or pulling into an empty (newly initialized) repository. As a noun, two repositories can be said to be clones if they are kept synchronized, and contain the same revisions.

  • Pull / push = Copy revisions from one repository into another. Pull is initiated by the receiving repository, while push is initiated by the source. Fetch is sometimes used as a synonym for pull, or to mean a pull followed by an update.

  • Merge = A merge or integration is an operation in which two sets of changes are applied to a file or set of files.

UNIT 2 VOCAB

  • Bits
    • A bit (binary digit) is the smallest unit of data that a computer can process and store. A bit is always in one of two physical states, similar to an on/off light switch. The state is represented by a single binary value, usually a 0 or 1. However, the state might also be represented by yes/no, on/off or true/false.
  • Bytes
    • In most computer systems, a byte is a unit of data that is eight binary digits long. A byte is the unit most computers use to represent a character such as a letter, number or typographic symbol. Each byte can hold a string of bits that need to be used in a larger unit for application purposes.
  • Hexadecimal / Nibbles
    • A nibble can also be represented by a hexadecimal digit. Hexadecimal is a base-16 numbering system that uses the digits 0 through 9 and the letters A through F to represent data, including nibbles and bytes. Figure 1 shows each possible bit combination in a nibble, along with its hexadecimal and decimal equivalent.
  • Binary Numbers:
    • Unsigned Integer
      • An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation. The most significant byte is 0 and the least significant is 3.
    • Signed Integer
      • A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation.
    • Floating Point
      • A floating point number, is a positive or negative whole number with a decimal point. For example, 5.5, 0.25, and -103.342 are all floating point numbers, while 91, and 0 are not. Floating point numbers get their name from the way the decimal point can "float" to any position necessary.
  • Binary Data Abstractions:
    • Boolean
      • In computer science, a Boolean is a logical data type that can have only the values true or false . For example, in JavaScript, Boolean conditionals are often used to decide which sections of code to execute (such as in if statements) or repeat (such as in for loops).
  • ASCII
    • ASCII (American Standard Code for Information Interchange) is the most common character encoding format for text data in computers and on the internet. In standard ASCII-encoded data, there are unique values for 128 alphabetic, numeric or special additional characters and control codes.
  • Unicode
    • Unicode is the universal character encoding used to process, store and facilitate the interchange of text data in any language while ASCII is used for the representation of text such as symbols, letters, digits, etc. in computers
  • RGB
    • RGB (red, green, and blue) refers to a system for representing the colors to be used on a computer display. Red, green, and blue can be combined in various proportions to obtain any color in the visible spectrum. Levels of R, G, and B can each range from 0 to 100 percent of full intensity.
c = 'p'
print("The ASCII value of '" + c + "' is", ord(c))
The ASCII value of 'p' is 112
bytesArr = bytearray(b'\x00\x0F')
  
# Bytearray allows modification
bytesArr[0] = 255
bytesArr.append(255)
print(bytesArr)
bytearray(b'\xff\x0f\xff')
 
print("The hexadecimal form of 23 is "
                            + hex(23))
                             
print("The hexadecimal form of the "
      "ascii value is 'a' is " + hex(ord('a')))
       
print("The hexadecimal form of 3.9 is "
                        + float.hex(3.9))
The hexadecimal form of 23 is 0x17
The hexadecimal form of the ascii value is 'a' is 0x61
The hexadecimal form of 3.9 is 0x1.f333333333333p+1

Python Data Types

Variable is a way of storing values into the memory of the computer by using specific names that you define.

  • Data types

    • Integer (int) = Whole number
    • Float (float) = Decimal number
    • String (str) = Text
    • Boolean (bool) = True / False
    • List (list) = A “container” that can store any kind of values. You can create a list with square brackets e.g. [1, 2, 3, 'a', 'b', 'c'].
    • Tuple (tuple) = A similar “container” as list with a difference that you cannot update the values in a tuple. You can create a tuple with parentheses (1, 2, 3, 'a', 'b', 'c').

Variables

A variable is created the moment you first assign a value to it

x = 5
y = "Ananya"
print(x)
print(y)
5
Ananya

Data Types

Variables can store data of different types, and different types can do different things.

x = 5
print(type(x))
<class 'int'>

Lists

Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

thislist = ["apple", "banana", "cherry"]
print(thislist)
['apple', 'banana', 'cherry']

Dictionaries

Dictionaries are .Python's implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value

Algorithms

Python algorithms are a set of instructions that are executed to get the solution to a given problem. Since algorithms are not language-specific, they can be implemented in several programming languages. No standard rules guide the writing of algorithms

myfamily = {
  "child1" : {
    "name" : "Emil",
    "year" : 2004
  },
  "child2" : {
    "name" : "Tobias",
    "year" : 2007
  },
  "child3" : {
    "name" : "Linus",
    "year" : 2011
  }
}

Sequence

In Python, sequence is the generic term for an ordered set. There are several types of sequences in Python, the following three are the most important. Lists are the most versatile sequence type.

Selection

In Python, the selection statements are also known as decision making statements or branching statements. The selection statements are used to select a part of the program to be executed based on a condition.

Iteration

Repetitive execution of the same block of code over and over is referred to as iteration. There are two types of iteration: Definite iteration, in which the number of repetitions is specified explicitly in advance.

Expressions

In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn't return anything.

Comparison Operators

A comparison operator in python, also called python relational operator, compares the values of two operands and returns True or False based on whether the condition is met

Booleans Expressions and Selection

A boolean expression (or logical expression) evaluates to one of two states true or false. Python provides the boolean type that can be either set to False or True. Many functions and operations returns boolean objects. The not keyword can also be used to inverse a boolean type.

Charcters

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

Strings

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

a = """HELLO HELLO
HELLOHELLO
HELLOHELLOHELLO
"""
print(a)
HELLO HELLO
HELLOHELLO
HELLOHELLOHELLO

a = "Hello"
print(a)
Hello

Length Concatenation

Concatenation means joining strings together end-to-end to create a new string. To concatenate strings, we use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.

Boolean Values

In programming you often need to know if an expression is True or False. You can evaluate any expression in Python, and get one of two answers, True or False.

print(10 > 9)
print(10 == 9)
print(10 < 9)
True
False
False

Python Operators

Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values:

print(10 + 5)
15

Tuple

Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.

thistuple = ("apple", "banana", "cherry")
print(thistuple)
('apple', 'banana', 'cherry')

Python Conditions and If statements

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • These conditions can be used in several ways, most commonly in "if statements" and loops.

An "if statement" is written by using the if keyword.

a = 33
b = 200
if b > a:
  print("b is greater than a/")
b is greater than a/

Python Loops

Python has two primitive loop commands: while loops for loops

i = 1
while i < 6:
  print(i)
  i += 1
1
2
3
4
5
i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1
1
2
3

Function

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

def my_function():
  print("Hello from a function")

Python ascii() Function

The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc). The ascii() function will replace any non-ascii characters with escape characters: å will be replaced with \xe5

More Vocab

  • Python For
    • What Are For Loops? In the context of most data science work, Python for loops are used to loop through an iterable object (like a list, tuple, set, etc.) and perform the same action for each entry. For example, a for loop would allow us to iterate through a list, performing the same action on each item in the list.
      • While loops with Range
    • You can use the range() method. Note: the values are from 0 to 4, not 0 to 5. The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at the specified number. In the above example, we start from 10, we terminate at 25 and we increment by 5.
      • with List
    • List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
      • Combining loops with conditionals to Break
    • In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered. You'll put the break statement within the block of code under your loop statement, usually after a conditional if statement.
      • Continue
    • The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.
      • Procedural Abstraction
    • The name of the function becomes an abstraction that can be used as we reason through a complex program. Our program becomes a more manageable set of functions, instead of a flat sequence of thousands of statements. It is easier to understand, build, and maintain
      • Python Def procedures
    • A procedure allows us to group a block of code under a name, known as a procedure name. We can call the block of code from anywhere in the program to execute the instructions it contains. We can also pass values to the procedure to change how it works.
      • Parameters
    • Parameters in python are variables — placeholders for the actual values the function needs. When the function is called, these values are passed in as arguments. For example, the arguments passed into the function .
      • Return Values
    • The value that a function returns to the caller is generally known as the function's return value. All Python functions have a return value, either explicit or implicit.
thislist = ["apple", "banana", "cherry"]
print(thislist)
['apple', 'banana', 'cherry']
a = 33
b = 200
if b > a:
  print("b is greater than a")
b is greater than a
i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1
1
2
3
i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)
1
2
4
5
6
i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i is no longer less than 6")
1
2
3
4
5
i is no longer less than 6
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
apple
banana
cherry
for x in "banana":
  print(x)
b
a
n
a
n
a
def my_function():
  print("Hello from a function")

my_function()
Hello from a function
  • Booleans Expressions and Iteration:
    • Certain forms of iteration (specifically while loops in Python) can use a boolean variable as a condition (similar to selection commands). As you may expect, the while loop executes commands until the value of the variable is false. In terms of a computer program, this could perhaps be used to execute commands critical for maintaining a certain process until that process is no longer needed. This could also potentially be used to periodically send notifications until a certain condition is met Truth Tables: A table for a logical operator (ex: AND, OR, XOR) containing each variable and all possible input and output results of that operator.
  • Truth Tables
    • A truth table has one column for each input variable (for example, p and q), and one final column showing all of the possible results of the logical operation that the table represents.
  • Characters: Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.