Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19167df

Browse files
Standard library and modules
1 parent 2da4a48 commit 19167df

File tree

4 files changed

+113
-11
lines changed

4 files changed

+113
-11
lines changed

‎basic_data_types/numbers.py‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
# NUMBERS
33
#####
44

5+
56
num_1 = 3
67
num_2 = 3.14
78

9+
10+
print(num_1)
811
print(type(num_1)) # Type Int
912
print(type(num_2)) # Type Float
1013

@@ -17,37 +20,41 @@
1720
# Multiplication: 3 * 2
1821
# Division: 3 / 2
1922
# Floor Division: 3 // 2 # Removes the decimal part
20-
# Exponent: 3 ** 2
23+
# Exponent: 3 ** 4
2124
# Modulus: 3 % 2
2225

2326
# Try
24-
print(3 % 2)
27+
print('Prova operazioni base:')
28+
print(7 % 4)
2529

2630
# Arithmetic variables
27-
num = 1
28-
num = num + 1
29-
print(num) # 2
31+
num = 3
32+
num = num + 4
33+
print('Prova somma num = num + 1:')
34+
print(num) # 7
3035

31-
num = 1
32-
num +=2
33-
print(num) # 3
36+
num = 2
37+
num *=4
38+
print(num) # 8
3439

3540
print('\n')
3641

3742
# Absolute value
38-
print(abs(-10)) # 3
43+
num = -10
44+
num = abs(num)
45+
print(num) # 10
3946

4047
# Rounding
4148
print(round(3.75))
4249
print(round(3.45))
4350
print(round(3.75, 1))
44-
print(round(3.768, 2))
51+
print(round(3.762, 2))
4552

4653
print('\n')
4754

4855
# COMPARISONS:
4956
# Equal: 3 == 2
50-
# Not Equal 3 != 2
57+
# Not Equal: 3 != 2
5158
# Greater Than: 3 > 2
5259
# Less Than: 3 < 2
5360
# Grater or Equal: 3 >= 2
@@ -56,6 +63,7 @@
5663
num_1 = 3
5764
num_2 = 2
5865

66+
print('Comparisons:')
5967
print(num_1 == num_2) # Returns and prints a boolean
6068
print(num_1 != num_2)
6169
print(num_1 <= num_2)

‎modules/modules_usage.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#####
2+
# MODULES USAGE EXAMPLE
3+
#####
4+
5+
courses = ['Art', 'Maths', 'History', 'CS']
6+
7+
# Import the module and use an alias
8+
import my_module as m
9+
10+
# Use the module
11+
index = m.find_index(courses, 'History')
12+
print(index)
13+
14+
# Import a single function and a single variable
15+
from my_module import find_index, string
16+
print(string)
17+
18+
# Import all
19+
from my_module import *
20+
21+
22+
23+
# --------------------> <-------------------- #

‎modules/my_module.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#####
2+
# MY MODULE EXAMPLE
3+
#####
4+
5+
print('Module imported')
6+
7+
string = 'Test'
8+
9+
def find_index(structure, target):
10+
for i, value in enumerate(structure):
11+
if value == target:
12+
return i
13+
14+
return -1
15+
16+
17+
# --------------------> <-------------------- #

‎modules/standard_library.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#####
2+
# STANDARD LIBRARY
3+
#####
4+
5+
import sys
6+
# print(sys.path) # See the current sys path
7+
8+
# What will happen if we try to import a module from a different directory?
9+
# Python can't find it
10+
# We must add the path
11+
sys.path.append('~/ale/Desktop/my_modules')
12+
# print(sys.path)
13+
14+
# Or we can add to the PATH environment variable (Different from Linux to Windows)
15+
# ---------- Linux ----------
16+
# nano ~/.bash_profile
17+
# export PYTHONPATH="~/ale/Desktop/my_modules"
18+
19+
20+
# Standard Library is one of the best resources already written
21+
# EXAMPLE
22+
23+
# Work with random module
24+
import random
25+
26+
courses = ['Art', 'Maths', 'History', 'CS']
27+
random_course = random.choice(courses)
28+
print(random_course + '\n')
29+
30+
31+
# Work with math module
32+
import math
33+
34+
rads = math.radians(90) # pi/2
35+
print('{}\n'.format(math.sin(rads))) # 1
36+
37+
38+
# Work with time and date module
39+
import datetime
40+
import calendar
41+
today = datetime.date.today()
42+
print(today) # Today's date
43+
44+
print('{}\n'.format(calendar.isleap(2019))) # Leap year?
45+
46+
47+
# Work with os module
48+
import os
49+
50+
print(os.getcwd()) # Current working directory
51+
print(os.__file__) # Where the entire standard library is located
52+
53+
54+
# --------------------> <-------------------- #

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /