GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Chapter basic-datatypes · python043/full-speed-python@efe7851

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Makefile
  • chapters
    • basic-datatypes.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Makefile‎

Copy file name to clipboardExpand all lines: Makefile
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ epub:
55
metadata.yaml \
66
chapters/introduction.md \
77
chapters/installation.md \
8+
chapters/basic-datatypes.md \
89

910
pdf:
1011
pandoc -o full-speed-python.pdf \
1112
metadata.yaml \
1213
-H preamble.tex \
1314
chapters/introduction.md \
1415
chapters/installation.md \
16+
chapters/basic-datatypes.md \
1517

1618
clean:
1719
rm *.epub *.pdf
Collapse file

‎chapters/basic-datatypes.md‎

Copy file name to clipboard
+90Lines changed: 90 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Numbers and strings
2+
3+
In this chapter we will work with the most basic datatypes, numbers and strings. Start your Python REPL and write the following:
4+
5+
```Python
6+
>>> a = 2
7+
>>> type(a)
8+
<class 'int'>
9+
>>> b = 2.5
10+
>>> type(b)
11+
<class 'float'>
12+
```
13+
14+
Basically, you are declaring two variables (named "a" and "b") which will hold some numbers: variable "a" is an integer number while variable "b" is a real number. We can now use our variables or any other numbers to do some calculations:
15+
16+
```Python
17+
>>> a + b
18+
4.5
19+
>>> (a + b) * 2
20+
9.0
21+
>>> 2 + 2 + 4 - 2/3
22+
7.333333333333333
23+
```
24+
25+
Python also has support for string datatypes. Strings are sequences of characters (like words) and can be defined using single or double quotes:
26+
27+
```Python
28+
>>> hi = "hello"
29+
>>> hi
30+
'hello'
31+
>>> bye = 'goodbye'
32+
>>> bye
33+
'goodbye'
34+
```
35+
36+
You can add strings to concatenate them but you can not mix different datatypes, such as strings and integers.
37+
38+
```Python
39+
>>> hi + "world"
40+
'helloworld'
41+
>>> "Hello" + 3
42+
Traceback (most recent call last):
43+
File "<stdin>", line 1, in <module>
44+
TypeError: must be str, not int
45+
```
46+
47+
However, multiplication works as repetition:
48+
49+
```Python
50+
>>> "Hello" * 3
51+
'HelloHelloHello'
52+
```
53+
54+
## Exercises with numbers
55+
56+
1. Try the following mathematical calculations and guess what is happening: \((3 / 2)\), \((3 // 2)\), \((3 \% 2)\), \((3**2)\).
57+
58+
Suggestion: check the Python library reference at <https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex>.
59+
60+
2. Calculate the average of the following sequences of numbers: (2, 4), (4, 8, 9), (12, 14/6, 15)
61+
62+
3. The volume of a sphere is given by (4/3 * pi * r^3). Calculate the volume of a sphere of radius 5. Suggestion: create a variable named "pi" with the value of 3.1415.
63+
64+
4. Use the module operator (%) to check which of the following numbers is even or odd: (1, 5, 20, 60/7).
65+
66+
Suggestion: the remainder of \(x/2\) is always zero when \(x\) is even.
67+
68+
5. Find some values for \(x\) and \(y\) such that \(x < 1/3 < y\) returns "True" on the Python REPL. Suggestion: try \(0 < 1/3 < 1\) on the REPL.
69+
70+
## Exercises with strings
71+
72+
Using the Python documentation on strings (<https://docs.python.org/3/library/stdtypes.html?#text-sequence-type-str>), solve the following exercises:
73+
74+
1. Initialize the string "abc" on a variable named "s":
75+
76+
1. Use a function to get the length of the string.
77+
78+
2. Write the necessary sequence of operations to transform the string "abc" in "aaabbbccc". Suggestion: Use string concatenation and string indexes.
79+
80+
2. Initialize the string "aaabbbccc" on a variable named "s":
81+
82+
1. Use a function that allows you to find the first occurence of "b" in the string, and the first occurence of "ccc".
83+
84+
2. Use a function that allows you to replace all occurences of "a" to "X", and then use the same function to change only the first occurence of "a" to "X".
85+
86+
3. Starting from the string "aaa bbb ccc", what sequences of operations do you need to arrive at the following strings? You can use the "replace" function.
87+
88+
1. "AAA BBB CCC"
89+
90+
2. "AAA bbb CCC"

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.