The first 25% of any college-level Programming Fundamentals course covers data types, variables, operators, and decision structures. Hopefully the next two blog posts will give you a solid introduction to concepts and you can take it from there (Google is your friend! For more depth in paper-form Starting Out With Python by Tony Gaddis is a good resource too)

  • DATA TYPES - strings, numbers, boolean, arrays, objects(this)

  • VARIABLE DECLARATIONS - const, let, var

  • COMPARISON OPERATORS - ==, ===, <, >, !=, !==, <=, >=

  • LOGICAL OPERATORS - !, ||, &&


Variables and their Data Types

JavaScript:
const greeting = "Hello fellow coder"
const question = "What's your name?"
const age = 36
Python:
age = 35
favNumber = "What is your favorite number?"

A variable is defined as a name that represents a value stored in the computer’s memory. The values with quotes around them are string data types and the value that age is referencing is an int or integer or number data type.

In the JavaScript example, we’re using the key word const to declare our variables. In the Python example, we have no keyword before the variable declaration. It is different for all languages.

const refers to a constant variable = one whose data type will not change throughout our program. The examples below will illustrate why this is important in JavaScript, and why it’s unnecessary in Python:

In JavaScript, we can concatenate strings and integers together like so:

const result = greeting+age;
console.log(result);
>>> "Hello fellow coder36"

Because JavaScript assumes what we want, and it converts it all to a string and saves it to the result variable, so we’ll get "Hello fellow coder36"

If we don’t concatenate (use the + sign) but use a , comma to dilineate arguments within the console.log(), we’ll print out two different values of two separate data types:

console.log(greeting, age)
>>> "Hello fellow coder" 36

If we try to concatenate a string and a number in Python, this would return a TypeError:

print(favNumber + age)
TypeError: can only concatenate str (not "int") to str

This is one reason why data types are important to understand in each language!

Operators: Logical and comparison

We’ve seen use of the = sign. This sign literally assigns a value to a variable or a different value. We’re only going to use variables that we have assigned values to (though you’ll see this isn’t always the case in JavaScript)

You can use == or === to make comparisons between values. These are read as completely different operators than the = (single equals sign).

# First, assign a value to the variable
age = 36.5
# Then, compare it with another value
print(age == 35)
>>> false

Since we’re using ==, it compares the value of age which is 36.5 to the int 35. Since they’re not the same value, it returns false.

Keyword rule

Since all programming languages have keywords (I introduced JavaScript’s const above) you should not use key words as variable names. At first you may do this accidentally, but as you use a language you’ll learn which are reserved words to avoid and how to use them properly.

Floating point and integer division

One point to be careful of is float division and integer division. In JavaScript, there is only one divisor symbol /

In Python, there is one symbol for floating-point division: / and another for integer division: //

# Divide two integers using the floating point divisor
5 / 2
# Get a floating point answer
>>> 2.5
# Divide two integers using the integer point divisor
5 // 2
# Get an integer answer
>>> 2

As you see, it chops of the .5 for integer division. This is not precise if you’re doing calculations!

Python

# Is the first evaluation the same as the second?
(5//2) == (5/2)
# Is the first eval less than the second?
(5//2) < (5/2)
# Is the first eval more than the second?
(5//2) > (5/2)
# Does the first eval not equal the second?
(5//2) != (5/2)

The only ones that would print true are the second statement and the last statement.

The use of ! is both involved in a comparison with != not equal to, and logical operator !. In Python, it’s literally the word not. Just like && in JavaScript is the word and in Python!

Javascript

// Using the modulus operator
const leftover = 17 % 3
>>> 2
// Incorperating !
!leftover
>>> false

Since leftover contains the integer 2, it’s a truthy value in JavaScript. Therefore its opposite is false.

Falsy values are as follows: false, 0 (zero), '' or "" (empty string), null, undefined, NaN

Javascript

// Using the modulus operator
const leftover = 10 % 2
>>> 0
// Incorperating !
!leftover
>>> true

Happy coding!

E


Got Questions❓, Comments 🗣 or Edits ✍

Use the Twitter thread below and hashtag #E4Everything to get in touch with me regarding this blog post:

Previous: Anatomy of CodeNext: Decision Structures