-
Notifications
You must be signed in to change notification settings - Fork 1
Python Basics
We will be introducing the basics of the Python programming language. Python is a powerful language used by Software Engineers today as a general purpose tool (e.g. Scripting, Scientific Programming, Website Backend, and Game Development to name a few). Specifically we will be addressing, printing to the screen, expressions, data types, string concatenation, and storing values into variables.
print() is a Python function that allows us to display messages on the screen. For example:
>>> print("Hello World")
Hello World
>>>
Python can be used to interpret mathematical expressions.
>>> 2 + 2
4
>>> 5 / 5
1
Operator | Name | Example |
---|---|---|
+ | Addition | 5 + 5 = 10 |
- | Subtraction | 10 - 2 = 8 |
* | Multiplication | 4 * 3 = 12 |
/ | Division | 4 / 2 = 2 |
// | Integer Division | 9//2 = 4 |
** | Exponentiation | 3**2 = 9 |
% | Modulus | 5 % 3 = 2 |
Computers work by manipulating data but different kinds of data need to be handled in different ways. Data types refer to the kind of information that needs to be processed. There are many data types but we will be focusing on three of them for now: integers, floating-point numbers, and strings.
An int data type is an integer; a whole number that may be positive, negative, or zero. e.g. 3 or -10
A float data type is a decimal number. e.g. 3.14 or 4.2
A string data type is a set of characters and is represented by being in either between single quotes (e.g. 'hello world') or double quotes (e.g. "Hello" or "24").
String concatenation is when you combine two strings to make a single string. Strings can be concatenated using the +
sign.
>>> print("Hello" + "world")
Helloworld
What happens if you try to concatenate a number to a string?
>>> print("My favorite number is " + 13)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
In Python, string concatenation only works with other strings. Since 13
is a number and not a string, this produces an error.
To concatenate a number to a string, you can convert a number to a string data type using the str()
function.
>>> 13
13
>>> str(13)
'13'
str(13)
returns a string representation of the number 13.
We can use str()
to fix the error in the previous example:
>>> print("My favorite number is " + str(13))
My favorite number is 13
A variable is used to store information in a program. You can think of a variable as a box, and we can store data inside the box. The box has a specific name that the developer chooses. Variable names should be specific and relevant so other developers can easily understand what information it is storing.
In mathematics, the equal sign =
means equality, but in programming languages it means assignment. We're "assigning" a value to the variable.
For example:
>>> myName = "Bob"
>>> myName
'Bob'
What happens to the old value when we assign the variable something new? The old value gets replaced, and the variable now holds the new value.
Here is where we introduce one of Python's pre-made functions, input(). Python includes a library of code already written by developers to be used by everyone, allowing the language to be extensible and make Python a more complete tool.
input() is a function that allows the user to input a string on the Terminal and allow the Python program to interact with it. Think of the input() function as a black box where you don't need to worry about how it works, only what it does
For example:
>>> name = input()
Diddy Kong
>>> print('My name is ' + name)
'My name is Diddy Kong'
Download the SimpleCalculator.py program and let's identify/debug the basic concepts