Day 2 Flashcards
Pretend you are a computer. What will you do with the following?
print(“Bonoboboude”[0])
B
The first of anything is always at position 0.
What is subscripting?
Pulling out a particular element from a string at the [x] position
What is an integer?
A whole number (without decimal places). Maximum size is: -2147483648 through 2147483647
(214 million)
e.g.
1234567
What is a float?
A Floating Point Number
A number with decimal places
e.g.
3.14259
What is a Boolean used for?
To test if something is True or False
How can you check the type of data?
print(type(example))
What is type conversion/type casting?
Change a piece of data from one type to another
In Python, what is used to
a) multiply
b) divide
c) get the exponent?
a) *
b) /
c) **
What is the order of priority of calculations?
PEMDAS
Parentheses ( )
Exponents **
Multiplication * Division /
Addition + Subtraction -
What is the output of this code:
print(len(12352))
TypeError: object of type ‘int’ has no len().
This happens because len() is a function that only takes a string input.
What is meant by declaring and initializing a variable?
The short answer is:
Declaring a variable means stating its name and data type. e.g: String NameOfString
Initializing means assigning a value to a variable. e.g:
NameOfString = “SomeNameValue”
Context:
Generally in programming declaring a variable means defining its type, and optionally, setting an initial value (initializing the variable). Variables do not have to be initialized (assigned a value) when they are declared, but it is often useful.
Python is a bit different.
Unlike in the C programming language, in Python a variable is not bound to a certain object. Rather, variables in Python work as pointers to objects. This means they’re not restricted to a specific type and can be assigned dynamically. For instance, you can assign a value of one data type to a variable, and to another data type directly afterward:
x = 5 x = "five"
This would throw an error in a statically typed language like C or Java. In those languages, a variable’s type has to be specified before it may be assigned to a value. This process is called variable declaration. But in Python, we don’t declare variables, we simply assign them. In short, we can think of a variable in Python as a name for an object.
What is meant by a statically typed language vs a dynamically typed language? which is Python?
Short Answer:
Static Type:
A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is;
Dynamic Type:
A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference).
Context:
Statically typed languages
A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is; other languages (e.g.: Java, C, C++) offer some form of type inference, the capability of the type system to deduce the type of a variable (e.g.: OCaml, Haskell, Scala, Kotlin).
The main advantage here is that all kinds of checking can be done by the compiler, and therefore a lot of trivial bugs are caught at a very early stage.
Examples: C, C++, Java, Rust, Go, Scala
Dynamically typed languages
A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference).
Examples: Perl, Ruby, Python, PHP, JavaScript, Erlang
Most scripting languages have this feature as there is no compiler to do static type-checking anyway, but you may find yourself searching for a bug that is due to the interpreter misinterpreting the type of a variable. Luckily, scripts tend to be small so bugs have not so many places to hide.
Most dynamically typed languages do allow you to provide type information, but do not require it. One language that is currently being developed, Rascal, takes a hybrid approach allowing dynamic typing within functions but enforcing static typing for the function signature.
How can you write a long number with decimals as an int in Python?
instead of 123,456,789 you can write 123_456_789. The python interpreter will take out the underscores and read it as 123456789.
What is a Function?
Its a piece of code that performs a specific operation. It can take an input like:
len(“test”) # “test” is the input and the len function calculates the length
How can you convert a int to a string?
str(123)