Skip to content

Commit 68f76f2

Browse files
committed
Split foundations lesson to two lessons and changed the order of lessons
1 parent e963852 commit 68f76f2

5 files changed

Lines changed: 528 additions & 393 deletions

File tree

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
id: output-variable-datatypes
3+
title: Outputting, Storing and Representing Data
4+
sidebar_label: 01.01 - Outputting, Storing and Representing Data
5+
sidebar_position: 1
6+
lesson: true
7+
---
8+
# Outputting, Storing and Representing Data
9+
Welcome to the first real python lesson. Our programs need a way to output information back to users and they also need to store various data to perform operations on them. We will learn how to do these things in the following lesson.
10+
11+
## Lesson Overview
12+
At the end of the lesson you will:
13+
* Know how to output information to users screen
14+
* Know what is a **variable** and how to store values to them
15+
* Know core python data types:
16+
* Integer
17+
* Float
18+
* String
19+
* Boolean
20+
* Know how to represent missing values
21+
22+
23+
## Outputting Data Back to User
24+
In Python, we use `print` function to output some data back to user.
25+
```python interactive
26+
print(5)
27+
```
28+
Whatever you pass into the function will be outputted to the screen.
29+
30+
We can also *print* multiple things at once, with python inserting space between each **argument**.
31+
```python interactive
32+
print(2, 5)
33+
```
34+
35+
## Declaring Variables
36+
You can think of **variables** as boxes where your program stores various data. Python does not require any special syntax for declaring variables.
37+
38+
**Variables** are defined by *assignment*.
39+
```python
40+
my_variable = 5
41+
```
42+
43+
Declaring variables has some rules:
44+
* It must start with a letter or underscore
45+
* It can contain lowercase and uppercase letters, underscores, and numbers
46+
47+
Styling guide **PEP8** suggests the use of `snake_case` for variable naming in python.
48+
49+
:::tip
50+
51+
See [PEP8 - Style Guide for Python Code](https://peps.python.org/pep-0008/) to learn more about style of writing python code.
52+
53+
:::
54+
55+
## Python Core Data Types
56+
Python has 4 core data types with addition of `None` which is a special value representing **no value**.
57+
58+
:::note
59+
60+
Python has a special `type()` function which returns what type some data is. We will use this to inspect the following data types.
61+
62+
:::
63+
64+
### Integer
65+
Integers represent **whole numbers**. We use them to represent age of user, score of the game and similar things.
66+
67+
In Python, they are represented as `int`.
68+
```python interactive
69+
my_int = 5
70+
71+
print(type(my_int))
72+
print(my_int)
73+
```
74+
75+
### Float
76+
Floats represent **real numbers**. We use them to represent prices in shop and various other purposes.
77+
78+
In Python, they are represented as `float`
79+
```python interactive
80+
my_float = 1.25
81+
82+
print(type(my_float))
83+
print(my_float)
84+
```
85+
86+
### String
87+
Strings represents **text** data. We use them to represent names, titles, descriptions and various other purposes.
88+
89+
In Python, they are represented as `str` and must be wrapped with single (`'`) or double (`"`) quotes.
90+
```python interactive
91+
string1 = "Double quoted string"
92+
string2 = "Single quoted string"
93+
94+
print(type(string1), type(string2))
95+
print(string1, string2)
96+
```
97+
98+
**Strings** can have special characters called **escape character**. That character is a single **backslash** (`\`). Most common usecase is `\n` which tells Python that the rest of the string goes on the new line.
99+
```python interactive
100+
a = "Hello \n World"
101+
102+
print(a)
103+
```
104+
105+
### Boolean
106+
Boolean values represent **yes** or **no** state. We use them to represent if something is *true* or *false*.
107+
108+
In Python, they are represented as `bool` and only two valid values are `True` or `False`.
109+
```python interactive
110+
bool1 = True
111+
bool2 = False
112+
113+
print(type(bool1), type(bool2))
114+
print(bool1, bool2)
115+
```
116+
117+
### None
118+
This special value `None` represents when there is no value. Its *type* is a special **NoneType**. Its useful when some of the data is missing or arriving at a later time.
119+
120+
```python interactive
121+
none_value = None
122+
123+
print(type(none_value))
124+
print(none_value)
125+
```
126+
127+
### Complex, Bytes, Bytearray
128+
Python has a built-in support for **complex** numbers, as well as for **bytes** representations, but we will deal with those later.
129+
130+
## Type Casting
131+
Type casting refers to transforming one datatype to another. For example, to transform string `"32"` to integer `32`.
132+
```python interactive
133+
a = "32"
134+
print("Type:", type(a), "Value:", a)
135+
b = int(a)
136+
print("Type:", type(b), "Value:", b)
137+
```
138+
139+
We can also convert to and from other types.
140+
```python interactive
141+
a = 5
142+
b = str(a)
143+
c = float(a)
144+
145+
print("a=", a, "type=", type(a))
146+
print("b=", b, "type=", type(b))
147+
print("c=", c, "type=", type(c))
148+
```
149+
150+
:::note
151+
152+
If Python cannot convert one type to another, it will raise **ValueError**. This usually happens when you try to convert some *text* into *number* or *float*.
153+
154+
Error are very helpful as they tell us exactly **what** is the issue and **where** they happened. If the errors did not exists, your programs would just silently die, and you would have to randomly guess where the issue is.
155+
156+
We will learn how to read and deal with errors in later lessons.
157+
158+
:::
159+
160+
## Assignment
161+
Try the following exercise to establish your knowledge.
162+
We will define a *shop name* and *item* with various data types.
163+
1. Create a variable called `shop_name` and set its value to some **string**
164+
2. Create a variable called `item_name` and set its value to some **string**
165+
3. Create a variable called `item_quantity` and set its value to some **integer**
166+
4. Create a variable called `item_price` and set its value to some **float**
167+
5. Create a variable called `item_available` and set its value to some **boolean**
168+
6. Output all of these variables declared to the screen (you can use multiple print statements) following this format: `<variable name>: <variable value>`
169+
170+
### Assignment Area
171+
Use our built in Python interpreter to solve your assignment.
172+
```python interactive
173+
# Write your assignment below
174+
175+
```
176+
177+
## Deepen Your Knowledge
178+
Go through these articles to deepen your knowledge about the topics covered in this lesson.
179+
1. Learn more about [Strings in Python](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str) in **Official Python Documentation** and [What is String and its types in Python](https://dev.to/atifwattoo/what-is-string-and-its-types-in-python-4ide) from this article from **dev.to** .
180+
2. Read more about `print()` function from [Your Guide to Python print Function](https://realpython.com/python-print/) from this **RealPython** article.
181+
3. Learn more about [Basic Datatypes in Python](https://realpython.com/python-data-types/) from this **RealPython** article.
182+
183+
## What's Next
184+
Now that you have basic knowledge of Python's core data types, we will see how we can use them to make useful output. In the next lesson, we learn about basic **math** and **comparisons**.

0 commit comments

Comments
 (0)