Le1 - Language - Basics - Jupyter Notebook
Le1 - Language - Basics - Jupyter Notebook
Hello, World!
Line 1: Referred to as a comment in software development. The Python interpreter ignores every line that starts with a # . Comments are used to explain what you're code is doing to other humans.
Line 2: The print() command is a function. Specifically referred to as a built-in function. These are functions you do not have to import to use. The print() function is used to output characters to the
console/terminal when Python programs are running. Let us consider some more examples:
2.0
12
Note:
The statement x = 12 should be interpreted as "assigning 12 to x" because we are instructing the computer to hold 12 in a memory location with the alias 'x', it should not be interpreted as "x is equal to 12"
Formally, variables in programming are small containers in memory that hold specific values. Values can be of many different types:
• numbers: integers, real numbers (which include numbers with decimal parts, known as floating-point numbers). There are 3 major numeric types in Python: int , float , and complex
• strings: combination of characters such as alphabets, numbers, and other characters. Strings in Python can be denoted by enclosing the content of the strings with single quotes: '' , double quotes "" , triple single
quotes ''' or triple double quotes """ .
• bool: The boolean data type has two possible values: True and False .
• other types such as list , dict , tuple , set , NamedTuple , etc.
• user-defined types: you can also create your own type for specific needs that do not align perfectly with the types that exist in the language. For example, other Python users have created types such as
pandas.DataFrame , numpy.array , that are very beneficial to Python's scientific computing community.
Eventhough a variable can hold values of any of the above-mentioned types. However, Python is flexible in that you do not have to explicitly declare the type of values a variable is going to hold when you create it.
In [6]: my_variable = 'assigned the value of a string'
You can also change the type of value a variable holds after it has been created:
34
<class 'int'>
now a string
<class 'str'>
Programming languages where the type of variables can be changed after they've been created are referred to as dynamically typed language. This behavior is possible because the variables are assigned a type as the
code is being executed. Python is a dynamically typed language. While the ability to alter the type of a variable offers much more flexibility, it also makes the code less optimized (the interpreter has to always check the
type of a variable at any point in the code it's assigned a new value. This contributes to slower program speeds of dynamically typed languages).
There is another group of programming languages where the programmer is required to state the data type of each variable when the variable is being declared. The data types are checked as programs written in these
languages are compiled before the programs can be used. Such languages are referred to as statically typed languages. For these languages, a variable's data type is static and cannot be altered, or a compilation error
will be raised.
In [9]: # To obtain the floored quotient (might also be referred to as integer division)
x = 19
y = 5
floored_quotient = x // 5
print(f"Floored quotient: {floored_quotient}")
Floored quotient: 3
x divided by y is equal to 3 remainder 4
In [10]: # Calculating power
# 2 raised to power 3
print(2**3)
# 2 raised to power 10
print(2**10)
# 7 raised to power 2
print(7**2)
8
1024
49
In [11]: comp_1 = 4 + 3j
print(comp_1)
(4+3j)
In [12]: comp_1 = 4 + 3j
comp_2 = 6 - 2j
Strings
We have used strings to print out user-friendly versions some mathematical expressions above. Let us take a closer look at how they work in Python.
As stated above, strings can be expressed in several ways. They can be enclosed in single quotes ('...'), double quotes ("...") with the same result. `' can be used to escape quotes.
In [ ]: # We could also avoid all this by just re-writing the expression as so:
print("he wasn't welcomed back") # surround the entire string with double quotes
Escape sequences
Sequence Meaning
\\ backslash
\n Newline
\t tab
In [ ]: # Two strings can be added to each other. This is known as concatenation
name = input("Please enter your name: ")
greeting = "Hello " + name + " and welcome to Python"
print(greeting)
In [ ]: # Strings placed next to each other will also be automatically concatenated
another_greeting = 'Hello, ' 'I am new here.'
print(another_greeting)
In [ ]: # this feature will not work when joining strings literals and variables
note '. more notes'
Strings will be discussed in details later in the course when we look at data structures like lists and dictionaries.
A variable can be explicitly converted to another type. For example, we can convert collected_number above to a number:
In [ ]: converted_number = int(collected_number) # You can also use float(collected_number) if the user is expected to enter floating-point numbers
print(converted_number)
print(type(converted_number))
Comparison Operators
These are operators that are used to compare two values. Comparison statements are typically evaluated to True or False . In Python True and False are both of the data Boolean type.
Operator Meaning
== equal to
!= not equal to
In [ ]: