0% found this document useful (0 votes)
2 views

Udacity Intro to Python

The document provides an introduction to Python programming, covering key concepts such as case sensitivity, whitespace importance, and error messages. It includes tables of functions and methods for strings and lists, arithmetic and comparison operators, as well as details on data types like integers, floats, booleans, and tuples. Additionally, it explains variable assignment, logical operators, and the use of membership operators.

Uploaded by

My pig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Udacity Intro to Python

The document provides an introduction to Python programming, covering key concepts such as case sensitivity, whitespace importance, and error messages. It includes tables of functions and methods for strings and lists, arithmetic and comparison operators, as well as details on data types like integers, floats, booleans, and tuples. Additionally, it explains variable assignment, logical operators, and the use of membership operators.

Uploaded by

My pig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to Python Programming

1. Python is case sensitive.

2. Spacing is important.

3. Use error messages to help you learn.

4. Whitespace will not affect how your code works.

5. Limit your lines to 80 characters (no long lines!)

6. When we call a function or method, the value within the parentheses


is known as an argument.

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 ^)

Variables and Assignment Operators


- Variables can be easily set by writing their name down and what they
equate to.
o = is the assignment operator, which assigns the numerical term
to a variable.
- Assignment operators apply the arithmetic operator to the value on
the left, making your code more concise.
- If you attempt to print the value of a variable which is not defined you
will receive the error “NameError: name ‘_’ is not defined”
- Closely related variables can be defined in a single line, with each
variable corresponding to the numeric value on the right of the
assignment operator.
- Use ordinary numbers, letters or underscores when defining
variables. Use all lowercase letters and underscores to separate
words.
o They cannot contain spaces
o They must start with a letter or underscore.
o They cannot use reserved words or built-in identifiers.
- A variable can be assigned to itself and operators can be performed to
increment and redefine the variable.
o += or -= increments the variable on the left by value on the
right.

Integers and Floats


- An integer is a data type consisting of a whole number value without a
decimal point.
- A float (or floating point number) is a data type consisting of a real
number that uses a decimal point to allow numbers with fractional
values.
o Floating point numbers are approximations of the numbers
they’re supposed to represent. This is necessary because floats
can represent an enormous range of numbers so in order to fit
in computer memory, Python must use approximations.
o There are therefore small deviations from mathematically
accurate input values and the ones that Python uses.
- Every object will either be an integer (int) or a float (float), and we
can convert between these types of objects in Python.
- An operation involve an int and a float always produces a float.

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

Strings and Arithmetic Operators


- A string is a data type consisting of an immutable, ordered sequence
of characters.
o You can use single or double quotes to indicate a string.
o If quote marks are repeated in the string, a backslash can be
placed before the quote mark within the string.
- We can set a variable to be a string.
- We can use certain arithmetic operators, listed below, on strings.
Subtraction and division do not work with strings.

+ combines strings (no spaces added)


* repeats strings a number of times

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.

You might also like