Mon. Jan 23rd, 2023

Defining and Declaring Constants and Variables

Alphanumeric Strings

Not a data type per se; rather, a string (see below) which contains only letters and/or numbers. In code, any string is denoted by being enclosed within ” or ‘. Most languages use “.

Arrays

An array is used to store multiple pieces of data of the same type. Rather than create separate variables for each piece of information, a single variable is created which contains a number of elements (slots) into which different values can be stored. The word ‘array’ is often used interchangeably with ‘list’, although technically there are differences.

C#
int[] scores = new int[5];
scores[0] = 15;
scores[1] = 12;
scores[2] = 22;
scores[3] = 31;
scores[4] = 14;

//OR
int[] scores = new int[] {15, 12, 22, 31, 14};

//Both examples create an array (denoted by []) containing
//five elements. Note elements are numbered from 0 - 4, not 1 - 5
//Elements of the array are accessed by placing a value in [] 


Python
scores = []
scores.append(15)
scores.append(12)
scores.append(22)
scores.append(31)
scores.append(14)

#OR
scores = [15, 12, 22, 31, 14]

//Python actually creates lists, hence you append items onto the 
//list. Compare with the C# example where an array with 5 elements
//is created and values placed into the elements

For completeness, please note that in C# it is also common to use lists instead of arrays when you don’t know the maximum size, or the list size is likely to be variable:

C#
//List example
using System.Collections.Generic;
//Make sure namespace is included

List<int> scores = new List<int>();
//Create a list called scores, that will store integers
scores.append(15);
scores.append(12); //etc
scores[0] = 14; //can still access elements directly like in array

Boolean

A Boolean variable can take one of two values: true or false.

C#
bool b = false;
bool c = true;
bool d = !b;    
//d is set to NOT b; NOT FALSE is TRUE


Python
b = False
c = True
d = !b
#Note in Python true and false begin with capital letters

Characters

A character is a single symbol; that is, a letter, a number, a punctuation mark etc.

Characters can also be converted freely between integers and characters in both Python and C#, although the methods differ slightly. This is a helpful ability – for example a Caesar Cipher which relies on moving characters by a set amount when encrypting text.

C#

char c = 'A';
//Note use of single quote marks to denote a character
int d = (int)c;
//In C# you can convert between int and char simply.
//Useful for getting ASCII codes of a character or the character
//represented by an ASCII code

char m = 'B';
int n = (int)m + 5;
char o = (char)n;
//varible o is now "B + 5" which is 'G'


Python 
#Python is not strongly typed like C#, so a character
#declaration looks identical to a string declaration
#except there is only one character
c = 'A'

#Get the ASCII code using ord()
d = ord(c)

#Get the character represented by an ordinal using chr()
m = 'B'
n = ord(m) + 5
o = chr(n)
#As above, o is now "B + 5" which is 'G'

Date/Time

Most languages provide data types that represent dates and/or times. In addition, it is possible to compare dates, add and subtract them and so on.

C#

DateTime d = DateTime.Now; //Gets current date and time
d.Month; //The month
d.Date; //The day of the month
d.Year; //The year of the date

Floating Point (Real, Double, Decimal)

Floating point values (also called ‘real’) are non-integer. In strongly typed languages (C#, Java, C etc) there are also double-precision floating point numbers (‘double’) and numbers geared towards representation of decimal numbers in base 10 (‘decimal’).

C#
float a = 3.54f;  //f denotes a floating point value
double b = 2.44;
decimal c = 1.35;


Python
a = 3.54
#As Python is not a strongly typed language you simply assign a
#value to the variable

Integers

Integers are whole numbers and can be positive or negative.

C# 
int a = 45;
int b = -10;


Python
a = 45
b = -10

Objects

Objects are instances of a class. When you use an object oriented language, you define a class to represent something. The class is a blueprint, and is used for making instances – objects.

//Assume there is a class called 'Person'

C#
Person p = new Person();
Person p2 = new Person();
//This creates two variables, p and p2, both of which represent a 
//person - the attributes will likely be different, but both
//represent a person


Python
p = new Person()
p2 = new Person()
#As above

Records

Records are data structures which allow key-value pairs to be stored. For example, if you were writing an application that worked with a database of books, it would be useful to be able to group together the pieces of data about a book into a single entity – things like:

  • Author (string)
  • Publication date (date)
  • Title (string)
  • ISBN (integer)

This is what a record allows for. A record represents one item. You can combine arrays and records: an array of records of books for example would contain the details of lots of books.

Records allow for a cleaner approach to coding; the alternative would be to create an array for the authors, another array for the publication dates, and so on.

Sets

A set is a data structure that can not contain any duplicate values. Neither C# nor Python include a standard way to construct these.

However, from a theory perspective, a set:

  • Contains only one type of data
  • Contains one or more values
  • Can not contain any duplicate values

This makes sets useful for some applications:

  • Lists of users who are allowed access to a resource
  • Possible correct answers to a question

They are not useful for simple data storage. Consider storing boolean responses to a question – the set could not contain more than two responses (one true, and one false).

Strings

Strings are collections of multiple characters. In most languages, they can also be treated as an array of characters. Strings are denoted by being enclosed in either ” or ‘. Most languages use “.

C#
string s = "This is a string";
char c = s[0];
//c will be set to the first character of the string, 'T'


Python
s = "This is a string"
c = s[0]