-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython-2.py
More file actions
129 lines (74 loc) · 4.34 KB
/
Python-2.py
File metadata and controls
129 lines (74 loc) · 4.34 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
# -*- coding: utf-8 -*-
"""Untitled9.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1P46p1C_C41Wz6jwff7EA3UJBd9oRPydX
"""
BASIC DATA TYPES
"""Integers:
Integers or "ints" for short, are whole-numbered numberic values. Any positive or negative number (or 0) without a decimal is an integer in Python. Integers values have unlimited precision, meaning an integer is exact. You can check the type of Python object with the type() function.
"""
type(22)
"""Use the function isinstance() to check whether an object is an instance of a given type:
"""
# Check if 22 is an instance of type "int"
isinstance(22, int)
"""The code output True confirms that 22 is an int.
Integer support all the basic math operations we covered last time. If a math operation involving integers would result in a non-integer (decimal) value, the result is becomes a float:
"""
5/2 # Calculation is 0.4 so it is not a whole number
type(5/2)
"""Floats:
Floating point numbers or "floats" are numbers with decimal values. Unlike integers, floating point numbers don't have unlimited precision because irrational decimal numbers are infinitely long and therefore can't be stored in memory. Instead, the coputer approximates the value of long decimals, so there can be small rounding errors in long floats. This error is so muscle it usually isn't of concern to us, but it can add up in certain cases when making many repeated calculations.
Every number in Python with a decimal point is a float, even if there are no non-zero numbers after the decimal:
"""
type(2.0)
isinstance(0.2222, float)
"""The arithmetic operations we learned last time work on floats as well as ints. If you use both floats and its in the same math expression the result is a float:"""
4 + 2.0
"""You can convert a fload to an integer using the int() function:
"""
int(6.0)
"""Floats can also take on a few special values: Inf, -Inf and NaN. Inf and -Inf stand for infinity and negative infinity respectively and NaN stands for "not a number", which is sometimes used as a placeholder for missing or erroneous numerical values. """
type( float ( "Inf" ) )
type( float ( "NaN" ) )
"""Booleans:
Booleans or "bools" are true/false values that result from logical statements. In Python, booleans start with the first letter capitalized so True and False are recognized as bools but true and false are not. We've already seen an example of booleans when we use the isinstance() funciton above.
"""
type(True)
isinstance(False, bool) # Check if False is of type bool
"""You can create boolean values with logical expressions. Python supports all of the standard logic operators you'd expect:"""
# Use > and < for greater than and less than:
70 > 22
# Use >= and <= for greater than or equal and less than or equal:
22 >= 22
# Use == ( two equal signs in a row ) to check equality:
50 == 50
40 == 40.0 # Equivalent ints and floats are considered equal
# Use != to check inequality. ( think of != as "not equal to" )
6 != 4
# Use the keyword "not" for negation:
not False
# Use the keyword "and" for logical and:
(7 > 4) and (8 > 10)
# Use the keyword "or" for logical or:
(7 > 4) or (8 > 10)
((3 > 2) or (5 < 1)) and (not True)
"""You can convert numbers into boolean values using the bool() function. All numbers other than 0 convert to True:
"""
bool(1) and bool(0)
"""Strings:
Text data in Python is known as a string or "str". Surround text with single or double quotation marks to create a string:
"""
type("cat")
type('1')
"""Two quotation marks next to each other (such as "or"") without anything in between them is known as the empty string. The empty string often represents a missing text value.
Numeric data logical data are generally well-behaved, but strings of text data can be very messy and difficult to work with. Cleaning text data is often one of the most laborious steps in preparing real data sets for analysis.
None:
In Python, "None" is a special data type that is often used to represent a missing value. For example, if you define a function that doesn't return anything (does not give you back some resulting value) it will return "None" by default.
"""
type(None)
# Define a function that prints the input but returns nothing
def my_function(x):
print(x)
my_function("hello") == None # The output of my_funciton equals None