notice that when indexing right to left, the reason we start at "-1" and not "0" is because "0" is already taken...it is the first index when indexing from L-R. This prevents confusion
- Feb 2025
-
runestone.academy runestone.academy
- Jan 2025
-
runestone.academy runestone.academy
-
We need to say a bit more about the parameters. In the definition, the parameter list is more specifically known as the formal parameters. This list of names describes those things that the function will need to receive from the user of the function. When you use a function, you provide values to the formal parameters
During function creation/definition, the parameters of the function are officially called "formal parameters". However, as the user calls/uses the function, the parameters the user supplies to the function are called arguments/actual parameters
-
- Sep 2024
-
runestone.academy runestone.academy
-
more content: Que: should it not start at the 4th char 'a', and then count backwards to -10 char. doing this wud give a n a b ..ie reading backwards from the 4th char should capture a n a b...no?
Ans The confusion here comes from how Python's slicing works. Python's slice syntax [start:end] does not work in a way that it counts backward to the negative index unless you specify a negative step. Let me clarify:
When you do fruit[3:-10], Python will attempt to slice from index 3 (which is 'a') to index -10. However, in Python, -10 refers to an index 10 characters before the end of the string, and since the string "banana" only has 6 characters, the end index is out of bounds. Python slices don't loop backward by default when you specify negative indices. Instead, the range 3:-10 results in an empty range since -10 is before the start of the string. For what you want to achieve (reading backwards starting from the 4th character), you need to explicitly tell Python to step backward using a negative step.
For example, to reverse slice from index 3: print(fruit[3::-1]) This will start at index 3 ('a') and then move backward through the string, producing: 'ananb'
-
- Aug 2024
-
runestone.academy runestone.academy
-
A list is primarily a datatype, but it is also a collection (in the sense it contains a collection of items)
-
- Jul 2024
-
runestone.academy runestone.academy
-
The slicing fruit[3:-10] essentially translates to fruit[3:0] because Python normalizes negative indices relative to the length of the string. When start is greater than end in a forward slice, it results in an empty string.
-
- Jun 2024
-
runestone.academy runestone.academy
-
The crux or summary of this page is that instead of using the elements of a list to use a for-loop for iteration, you can use the 'range' function.
-
- Mar 2024
-
runestone.academy runestone.academy
-
A for loop is used to repeat an action until a specific condition is met. A common use of the for loop is to iterate over the elements of a collection as long as the collection is a sequence.
In Python, the terms "collection," "iterable," and "sequence" refer to different types of objects, each with its own characteristics and use cases. Here's a brief overview highlighting the main differences:
Iterable:
An iterable is any Python object you can get an iterator from. You can loop over an iterable using a for loop. Iterables define an iter() method that returns an iterator. They can also be materialized into lists or other collections if needed. Examples include all sequence types (like lists, strings, and tuples) and some non-sequence types like dictionaries, file objects, and sets. Collection:
A collection is a more general term that refers to any sizable and structured group of elements you can iterate over. In Python, collections are usually more specific and refer to the module collections or types that are inherently "container-like." Python's collection types include lists, tuples, dictionaries, and sets, but when referring to the collections module, it might mean specialized container datatypes like Counter, deque, OrderedDict, etc. Not all collections are sequences (for example, sets and dictionaries), but they are all iterables. Sequence:
A sequence is an ordered collection of items that allows you to access its elements via an index. Sequences are a subset of iterables. Sequences define both getitem() and len() methods. The presence of these methods supports indexing and slicing. Examples of sequences include lists, strings, and tuples. Unlike general collections or iterables, sequences always maintain the order of their elements. In summary, the key distinctions are:
All sequences are iterables, but not all iterables are sequences. Sequences have a specific order and can be indexed. Collections can be considered a broader category that includes all container types in Python, not necessarily ordered or indexable. All sequences are collections, but not all collections are sequences (e.g., sets and dictionaries are collections but not sequences).
-
- Jun 2023
-
runestone.academy runestone.academy
-
First, import any modules that will be required. Second, define any functions that will be needed. Third, define a main function that will get the process started. And finally, invoke the main function (which will in turn call the other functions as needed).
This is how I was taught to write C++ code
-
-
runestone.academy runestone.academy
-
deterministic¶A process that is repeatable and predictable
"can be determined"
-
-
runestone.academy runestone.academy
-
Note The module files must be in the same directory on your computer for Python to know how to import them automatically
so Important!!
-
-
runestone.academy runestone.academy
-
attribute¶Some state or value that belongs to a particular object. For example, tess has a color.
some characteristic that belongs to an object
-
-
runestone.academy runestone.academy
-
The first line tells Python to load a module named turtle. That module brings us two new types that we can use: the Turtle type, and the Screen type.
just as we have 'int' and 'str' types in python by default, importing libraries can bring us new 'types' into our code
-
-
runestone.academy runestone.academy
-
runtime errors
program crashes at runtime/excution time/compile time due to some error
-
syntax errors
using wrong syntax of the programming language. Program crashes
-
semantic errors
In this error the program does not terminate prematurely (crash), but runs. However it does not do what you want it to do (does not give correct logic)
-
-
runestone.academy runestone.academy
-
A compiler reads the program and translates it completely before the program starts running
A compiler compiles ALL the code first, then executes it, while an interpreter executes the code LINE by LINE
-