Udacity Intro to Python
Udacity Intro to Python
2. Spacing is important.
Function Table
print() displays input value as text in the output.
type() returns the type of an object.
int() changes an object to an integer by cutting off after the decimal
place with no rounding.
float() changes an object to a float by adding a .0 after the integer.
str() changes an object to a string.
len() returns the length of an object.
max() returns the greatest element in a list
min() returns the smallest element in a list
sorted() returns the list in ascending order, or in descending order if you
add the optional argument reverse=true
Method Table (Functions associated with
objects)
Strings
.title() returns a capitalized string.
.islower( checks if the characters in a string are lowercase.
)
.count() returns how many times the substring occurs in the string.
.format() substitutes the substring into the main string.
.split() returns a list that contains the words from the input string.
Lists
.join() takes a list of strings as an argument and returns a string
consisting of the list elements joined by a separator string. (\n
as the separator leads to a new line)
.append( adds an element to the end of a list.
)
Arithmetic Operators
+ addition
- subtraction
* multiplication
/ division
// integer division, rounding down the
answer to the nearest integer
% modulo, the remainder after
dividing
** exponentiation (not ^)
Bool
- A Boolean is a data type that can have the value true or false.
o Boolean algebra involves arithmetic of variables that contain the
values true or false.
Comparison Operators
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to
Logical Operators
and evaluates if both sides are true
or evaluates if at least one side is true
not inverses a Boolean type
List
- A data type for mutable ordered sequences of elements.
o That means we can change a variable within a list to redefine it
to something else.
- An index is an integer value that indicates the position of an element
in a list.
o The first term in a list is assigned the index 0, in a tradition
known as zero-based indexing.
o We can use negative indices to count back from the start of a
list, where the last term is assigned the index -1.
- Slicing is when indices are used to slice off parts of an object like a
string or a list. The lower bound is inclusive but the upper bound is
exclusive.
o You can omit the starting index if you slice from the beginning of
the list.
o You can omit the ending index if you slice up to the end of a list.
Membership Operators
in evaluates if object on left side is
included in object on right side
not in evaluates if object on left side is not
included in object on right side
Tuples
- A data type for immutable ordered series of elements.
o Tuples are like lists with fewer features, used when we have two
or more values that are closely associated with each other.