Rebyuwer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Python - Takes place automa cally during run me

between ONLY numeric value


- Development started in the 1980’s by Guido
- Float + integer = float
van Rossum
- Integer + string = error
- Supports a mul tude of programming
paradigm Modules
- Interpreted, very-high-level programming
- Programs are usually created by typing
language
defini ons into a separate file called a
Notable Features of python module or a script.
- Python module must be a text file with a .py
- Easy to learn
extension
- Cross-pla orm
- Open Source Programming Environment and IDLE
- Supports quick development
- A special type of so ware that simplifies the
- Extensible
process of crea ng modules/programs
- Embeddable
- Helps programmers write programs and
Literals include features such as automa c
inden ng, color highligh ng, and interac ve
print (“Hello”)
development
print (“geng geng”) - The standard python distribu on includes a
programming environment called IDLE that
- “Hello” and “geng geng” are the literals you can use for working on the programs of
- Used to indicate a specific value, which can this course
be assigned to a variable
Strings
Garbage Collec on
- Le ers, special characters, spaces, digits
- Python will automa cally clear old values - Enclose in quota on marks or single quotes
out of memory - “Borbs” “Acups”
Iden fiers Print
- Must begin with a le er or underscore, - Used to output stuff to console
which can be followed by any sequence of - Print(“sheesh”)
le ers, digits, or underscores
- Case-sensi ve Input(“ “)
- Keywords – part of python and cant be used
- Prints whatever is in the quotes
as ordinary iden fiers
- User types in something and hits enter and
Expression binds that value to a variable
- Text = input(“type anything”)
- Can produce new data (numeric or text) - Num = int(input(“type anything”))
values in your program using expressions.
- +, -, *, /, **, abs(), //, % Inden on

Data Conversion - Ma ers in Python


- How you denote block of code
while LOOPS Rela onal Operator

- Know numbers of itera ons - Evaluate a Boolean expression


- Can end early via break - < , <=, ==, >=, >, !=
- Uses a counter
In
- Can rewrite a for loop using a while loop
- Operator is used to determine if a specific
for LOOPS
value is in a given list
- Unbounded numbers for itera ons
Not in
- Can end early via break
- Can use a counter but must ini alize before - Operator returns the opposite result
loop and increment it inside loop
- May not be able to rewrite a while loop Two-Way Decisions
using a for loop - Can be implemented by a aching an else
Range (start, stop, step) clause onto an if clause.

- Default values are “start = 0” and “step = 1’ Mul -Way Decisions


and op onal - Use nested if-else statement to implement
- Loop un l value is stop – 1 mul -way decisions
Break - Else-if can be combined into elif

- Immediately exits whatever loop it is in Indefinite loops

Control Statement - Keeps itera ng un l certain condi ons are


met
- Is a statement that determines the control - if-else loop
flow of a set of statements
Definite loops
Control structure
- Number of itera ons is determined when
- Is a set of statements and the control the loops starts.
statements controlling their execu on. - for loop
3 fundamental forms of control Nested loops
- Sequen al - loop within a loop
- Selec on
- Itera on Pre-test loop

Boolean data type - one that checks for a condi on prior to


running the block of code
- Contains two Boolean values, denoted as
True and False in python Post-test loop

Boolean expression - execute the statements uncondi onally and


only check the condi on a er to determine
- Is an expression that evaluates to a Boolean if the statements should be executed again
value
Input valida on - Causes the variable to refer to the previously
bound variable in the closets enclosing
- If the user types an incorrect input, the
scope
programs ask for another value un l a valid
value has been entered Closure

Func ons - Closures are func on objects that


remembers values in enclosing scopes, even
- Sub-programs which performs tasks which
if they are no longer present in memory
may need to be repeated
- If you call closure without parentheses, only
- Similar to methods, but may not be
the type of the object will be returned
connected with objects
- Some func ons are bundled in standard The LEGB Rule
libraries
- Local
Func on Elements - Enclosed
- Global
- Define – Can appear at the beginning or end
- Built-in
of the program file
- Invoke or call – Usually happens in the body Lists
of the main() func on, but subfunc ons can
- Lists are one of the most powerful tools in
call other subfunc ons too.
python
Func on defini ons - They are just like the arrays declared in other
languages
- Two major parts: defini on head and
- Need not be always homogenous
defini on body
- Can contain strings, integers, as well as
- Defini on Head - has three main parts: the
objects in a single list
keyword def, the iden fier or name of the
- Can be used for implemen ng stacks and
func on, and the parameters in
queues
parentheses.
- Lists are mutable, they can be altered once
- Defini on body - The colon at the end of the
declared
defini on head marks the start of the body,
the block of statements Things we typically want to do with lists:

Variables - Retrieve (or set) an item at a specific


posi on via indexing
- References or pointers to an object in
- Concatenate two lists together
memory
- Get the length of the list
Global Keyword - Add an item to the list
- Remove an item from the list
- Telling python to use the globally defined - Check for membership
variable instead of locally defining it
Indexing
Nonlocal Keyword
- Can use listname[idx] to get and set an item
- Useful in nested func ons at a specific index
- Indexing syntax in python is quite powerful
- Can get a range of elements using what is - Returns the number of mes a specified
called a slice value occurs in a tuple
- Nega ve indices, which count from the back
Index()
of the list
- Can specify a third part of the slice called the - Searches the tuple for a specified value and
stride, by default the stride is one returns the posi on of where it was found
Len() Dic onaries
- Func on to get the length of a list. - Collec on of key-value pairs, unordered,
changeable and indexed
Pop()
- Wri en with curly brackets
- By default remove the first item in a list and
Get()
returns it.
- Get the value of a key
Remove()
Values()
- Remove items by value in a list
- Return values of a dic onary
Append()
Items()
- Add an item to the end of the list
- Loop through both keys and values
Insert()
Popitem()
- Add to posi ons other than the end, such as
the beginning - Removes the last inserted item
Extend() Del
- You can combine another list or tuple at the - The del keyword removes the item with the
end of the list. All items are added to the end specified key name
of the original list
Clear()
Clear()
- Emp es the dic onary
- Remove all items from the list
Copy()
Tuple
- Make a copy of a dic onary
- Is a sequence of immutable Python objects
- Tuple are immutable, they cannot be Dict()
changed once declared. - Also makes a copy of a dic onary
- Usually faster than lists

Tuple()

- Can make a tuple

Count()

You might also like