Categories
Coding TheMoreYouKnow What Is - X

WHAT IS – STATIC TYPING?

Updated 11/2/2020

The Gist of Static Typing

More languages today are based around Static Typing than Dynamic Typing. There are some languages that support both, but we won’t get into that here.
While statically-typed languages usually run faster than their dynamic brethren, they’re less trendy in the realm of web development and data science, although WebAssembly has been looking to shake up that game a bit. In general, these languages are better suited for games and applications, and won’t be going away any day soon!

Statically-typed languages are typically not beginner friendly, but that doesn’t mean they aren’t worth learning. Let’s jump in.

What is Static Typing?

It’s a little like this: Static Typing is a method where all variables must have a type, and that type cannot be changed during runtime.

Before we get confused here: the values of these static variables can change! They’re not like constants, whose values can’t change (and whose data types also can’t change, but I digress).

The only caveat is, quite predictably, that any new value assigned to the variable must be of the same type as the last value.


// In C++, we define a variable myInt of type int.
int myInt = 7; 

// This reassignment to another int value is okay!
myInt = 8; 

// This reassignment to a String is no good.
myInt = "JonOfOz";

Type checking happens at compile time. The compiler first verifies that the code doesn’t break any type rules, and if not, the program runs. Else, you’ll get type errors you need to fix first. This doesn’t guarantee you won’t run into errors, but you won’t run into ones related to type.

Static Typing has a number of benefits:
  • Static Typing results in faster, leaner code, since all data types are declared and checked at compile time.
  • There is less chance of running into errors in the middle of long processes due to simple mistakes. Imagine running a process that takes two days to finish, then bam, the code fails at hour 46 due to a single line where an integer was expected, but the string version of that integer gotassigned, and everything fell apart. Ouch.
Static Typing also has drawbacks:
  • Debugging can be frustrating in and of itself; that’s even more so the case with statically typed languages. The error messages can be pretty vague, and you’re dead in the water if you can’t start your debugger until you compile your code… which can’t compile until you fix all of the bugs. (You can get around this, but it can be a pain.)
  • Static typing is like an overprotective parent. If it sees you are about to learn a harsh lesson due to a type error, it won’t have it. This sort of sounds great, but giving the developer guardrails isn’t always a win: we learn a great deal from failures. So, what happens when we fail less?

How Does Static Typing Compare to Dynamic Typing?

For variables of both statically-typed and dynamically-typed languages, one principle is the same: whatever those variables are, they are references to addresses in memory.
In the case of the statically-typed example above, it’s referencing 7— more accurately, the memory address represented by 7.

The differences lies in the variables.

Static Typing

When a variable like myInt is declared, it is bound to the int data type for the duration of the runtime. The variable calls the shots: if it starts out as an int and you try to assign it a new String value, you’ll get an error.

Dynamic Typing

When a variable like myInt is declared, it’s nothing more than a reference to some address in memory. It might represent None / null, it might be an int, a String, it doesn’t matter. As long as you don’t perform illegal operations on whatever type it is at any point in runtime, you’re good to go.

Categories
Coding TheMoreYouKnow What Is - X

WHAT IS – PYTHON?

Updated 10/20/2020

The Gist of Python

I won’t hide it: Python is my favorite programming language of all time.


No, it’s not the fastest language out there—you’re not going to find it powering Triple A games or any graphics-intensive applications any time soon—but it can basically do everything really well and in fewer lines of code than other languages. And, in terms of readability and functionality, it hits a bullseye.

We won’t go into Python’s history here: there’s Wikipedia for that. I want to briefly explain what Python is, what makes it stand apart from most languages, and where it’s being used today. Let’s jump in.

What Python Is

Python is a programming language that is dead easy to learn, beautiful to look, and widely used—hey, you might be surprised where.

For starters, have some Python code!

> def say_hi():
>     print("Hello World!")
> hi()
"Hello World!"

Obviously, we could get more complex than that. Here, we just define a function and then we run it.

I just wanted to give a simple example to demonstrate the fact: Python is simple. So much so, it’s the most popular introductory programming language taught in universities worldwide.
The aforementioned study was done in 2014, but the momentum certainly hasn’t stopped. Udemy straight up advertises Python (above Javascript no less!) in their app store description. A large number of the most popular courses on Udemy are about Python and/or prominently feature Python as the language of choice for a number of different applications.

Udemy knows what you really want.

That leads us to another point: Python is general purpose. It can be used for creating websites and web apps; it can be crammed into integrated systems; and for Pete’s sake, it can power a Raspberry Pi to water your poor houseplants as you watch indifferently. Python is also popular for API development.

Where Python really excels, however, is in the realm of science.

Powerful libraries like Pandas, NumPy, SciPy, MatplotLib, Seaborn, Tensorflow, and others make data analysis, linear algebra, spreadsheet manipulation, and machine learning accessible (and relatively easy) to the general public by combining Python’s syntatic simplicity with common functions and routines found in each discipline.

What Makes Python Unique

Arguably the most unique thing about Python—and usually a dead giveaway that you’re looking at Python code—is its strict usage of indentation. By this, Python is said to adhere to the Off-side rule, a rarity among programming languages, with less than 2% of all languages using significant indentation.

Unlike the multifarious compiled languages, Python is an interpreted language: without going into the nitty-gritty details, this means code is compiled at runtime, not before. An implication of this is that your Python code doesn’t necessarily need to be error-free before you can run it, although Python won’t execute if, say, your code indentation is incorrect. which is a luxury you won’t get in a compiled language like C / C++ or Java something Compiled languages must be free: they won’t run if there is an error anywhere in the code

Where Python is Used Today

Forthcoming lol

Categories
Coding Python Secrets TheMoreYouKnow

The Arcane Secrets Of – PYTHON

Updated 10/13/2020

What We’ll Cover

  1. Various features in Python you may not have known about!
  2. These various features in action!
  3. Russian Roulette!

What We Won’t Cover

  1. Whether or not these features are considered by best practice.
  2. These features in both Python 2 and 3 (This site only covers 3).
  3. Any expenses related to playing Russian Roulette.
  • while loops and for loops can have else statements!
# RUSSIAN ROULETTE - Let's Play!
# (Seriously, don't.)
chamber_with_bullet = random.randint(1,6)
game_rounds = 3
dead = False
while not dead:
    # Spin the chamber, Charles
    chamber = random.randint(1,6)
    if chamber == chamber_with_bullet:
        dead = True
    else:
        print(f"BLAM: CHAMBER {chamber} IS SAFE!")
        game_rounds -= 1
        if game_rounds == 0:
            # The else block is skipped: you're alive!
            print("Congrats, pardner, you're alive!")
            break
else:
    print(f"BLAM: DEAD.\nThe bullet was in chamber {chamber_with_bullet}.")