-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathdata_types.py
More file actions
136 lines (100 loc) · 2.38 KB
/
data_types.py
File metadata and controls
136 lines (100 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import math
# String data type
# literal assignment
first = "Dave"
last = "Gray"
# print(type(first))
# print(type(first) == str)
# print(isinstance(first, str))
# constructor function
# pizza = str("Pepperoni")
# print(type(pizza))
# print(type(pizza) == str)
# print(isinstance(pizza, str))
# Concatenation
fullname = first + " " + last
print(fullname)
fullname += "!"
print(fullname)
# Casting a number to a string
decade = str(1980)
print(type(decade))
print(decade)
statement = "I like rock music from the " + decade + "s."
print(statement)
# Multiple lines
multiline = '''
Hey, how are you?
I was just checking in.
All good?
'''
print(multiline)
# Escaping special characters
sentence = 'I\'m back at work!\tHey!\n\nWhere\'s this at\\located?'
print(sentence)
# String Methods
print(first)
print(first.lower())
print(first.upper())
print(first)
print(multiline.title())
print(multiline.replace("good", "ok"))
print(multiline)
print(len(multiline))
multiline += " "
multiline = " " + multiline
print(len(multiline))
print(len(multiline.strip()))
print(len(multiline.lstrip()))
print(len(multiline.rstrip()))
print("")
# Build a menu
title = "menu".upper()
print(title.center(20, "="))
print("Coffee".ljust(16, ".") + "$1".rjust(4))
print("Muffin".ljust(16, ".") + "$2".rjust(4))
print("Cheesecake".ljust(16, ".") + "$4".rjust(4))
print("")
# string index values
print(first[1])
print(first[-1])
print(first[1:-1])
print(first[1:])
# Some methods return boolean data
print(first.startswith("D"))
print(first.endswith("Z"))
# Boolean data type
myvalue = True
x = bool(False)
print(type(x))
print(isinstance(myvalue, bool))
# Numeric data types
# integer type
price = 100
best_price = int(80)
print(type(price))
print(isinstance(best_price, int))
# float type
gpa = 3.28
y = float(1.14)
print(type(gpa))
# complex type
comp_value = 5+3j
print(type(comp_value))
print(comp_value.real)
print(comp_value.imag)
# Built-in functions for numbers
print(abs(gpa))
print(abs(gpa * -1))
print(round(gpa))
print(round(gpa, 1))
print(math.pi)
print(math.sqrt(64))
print(math.ceil(gpa))
print(math.floor(gpa))
# Casting a string to a number
zipcode = "10001"
zip_value = int(zipcode)
print(type(zip_value))
# Error if you attempt to cast incorrect data
# zip_value = int("New York")