veganism.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
Veganism Social is a welcoming space on the internet for vegans to connect and engage with the broader decentralized social media community.

Administered by:

Server stats:

214
active users

#data_question

4 posts3 participants0 posts today
Continued thread

#data_question
Use `normal` to generate normally distributed steps with some mean and standard deviation.

💡hint: edit your `steps` filter from the preceding question, with 5000 walks at 1000 steps each, to resemble this array
```
steps = np.random.normal(loc = 0, scale = 0.25, size = (nwalks, nsteps))
```

Continued thread

read returns a certain number of characters from the file.

The read method advances the file handle’s position by the number of bytes read.

> f.read(int(some_num_bytes))

tell gives you the current position

> f.tell()

If 10 characters is read from the file, the position would be 11 because it took that many bytes to decode 10 characters using the default encoding. You can check the default encoding in the sys module

#data_question

Import the sys module and run

> sys.getdefaultencoding()

Continued thread

📋You can control the amount of context shown using the %xmode magic command, from Plain (same as the standard Python interpreter) to Verbose (which inlines function argument values and more).

📋you can step into the stack (using the %debug or %pdb magics) after an error has occurred for interactive post-mortem debugging.

#data_question

Write a function to cast an input as a float with error handling for type and value. If it is a TypeError, return the input as a string.

Continued thread

🛠 Currying is computer science jargon (named after the mathematician Haskell Curry) that means deriving new functions from existing ones by partial argument application.

Having a consistent way to iterate over sequences, like objects in a list or lines in a file, is an important Python feature. This is accomplished by means of the iterator protocol, a generic way to make objects iterable. For example, iterating over a dict yields the dict keys. (Image 6)

#data_question ⭐

What is happening?

Continued thread

Given:

> def remove_punctuation(value):
> ... return re.sub('[!#?]', '', value)

You can use functions as arguments to other functions like the built-in map function, which applies a function to a sequence of some kind.

> for state in map(remove_punctuation, states):
> ... print(state)

#data_question

In your own words, what is happening in the loop?

Continued thread

#data_question

Suppose we have a list of lists containing some English and Spanish names. (Image 7)

You might have gotten these names from a couple of files and decided to organize them by language.

Now, suppose we wanted to get a single list containing all names with two or more e’s in them

Write a for loop to accomplish this task. Be mindful of computation speed

#data_question ⭐

You can actually wrap this whole operation up in a single nested list comprehension. What would it look like?

Continued thread

📋insert is computationally expensive compared with append because references to subsequent elements have to be shifted internally to make room for the new element. If you need to insert elements at both the beginning and end of a sequence, you may wish to explore collections.deque, a double-ended queue, for this purpose.

The inverse operation to insert is pop, which removes and returns an element at a particular index.

#data_question

What is the result:

> my_list.pop()

Continued thread

> my_list = ['foo']

Elements can be appended to the end of the list with the append method.

> my_list.append('baz')

Using insert, you can insert an element at a specific location in the list.

> my_list.insert(1, 'bar')

#data_question

In common terms, what did that insert command do?