Mike Driscoll Profile picture
Jul 24 β€’ 5 tweets β€’ 3 min read
You need to know the basics of #Python to be able to use it effectively.

So let's talk about how you can slice a list. List slicing is where you use square brackets to "slice" out a subset of the list.

A slice is a portion of a list returned as a new list

πŸ§΅πŸ‘‡ Image
Here is an example of slicing a #Python list: Image
You can also use negative values when slicing a #Python list

This example demonstrates using a negative value for the beginning of the slice while ALSO not specifying the end of the slice Image
You don't need to specify the beginning of a #Python list slice either.

In this example, I want to grab all the values starting at index 0 up to but not including index 3. Image
What do you think happens when you don't specify a beginning or an ending in a #Python list slice? Image

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

Jul 26
Today we are going to talk about environment variables with #Python!

This will be a thread, so prepare yourself!

πŸ§΅πŸπŸ‘‡
Environment variables are variables that are available to all your programs and saved in memory by your operating system.

You can get a listing of your environment variables using #Python's `os.environ` attribute:

🐍πŸ”₯ Image
The `os.environ` call returns a #Python dictionary.

This allows you to get environment variable values using dictionary access, like this: Image
Read 6 tweets
Jul 23
Today I am going to show you how to create 7️⃣ different file types with #Python:

🐍 Text file
🐍 CSV
🐍 XML
🐍 JSON
🐍 Tar file
🐍 PDF
🐍 Excel

Join me for this fun πŸ§΅πŸ‘‡
1️⃣ Creating a text file with #Python is a breeze! The recommended method is to use Python's `with` statement as it will automatically close the file for you

That means you can create a text file with TWO lines of code if you want to:
2️⃣ Creating a CSV file with #Python can be accomplished using the `csv` module

Here's a small example:
Read 16 tweets
Jul 20
When using a class in #Python, you may notice that the first argument in all the class's methods is "self"

What is up with that? Well, let's talk about it!

πŸ§΅πŸ‘‡πŸ
First of all, "self" is a convention that refers to the instance that is created when you instantiate a class. You don't have to name it "self", but that is best practice

Here is an example where "self" is replaced with "this" Image
You use "self" to be able to access instance attributes across methods in a #Python class.

Here is an example where you initialize (__init__) `self.name` and then use `https://t.co/O7h9e3XIVz` later on when you call the `walk()` method: Image
Read 6 tweets
Jul 13
Logging in #Python is a popular topic and it's a really useful one to understand.

Today we are going to learn more about that!

πŸ§΅πŸπŸ‘‡
Python includes the `logging` module as a part of its standard library.

You can get started using it quite easily:
Python's `logging` module supports multiple logging levels:

🐍 notset
🐍 debug
🐍 info
🐍 warning
🐍 error
🐍 critical
Read 17 tweets
Jul 12
Did you know you can configure #Python's logging module using a file config or a dictionary?

Find out how in this mini-thread!

Code for these examples can be found here: blog.pythonlibrary.org/2012/08/02/pyt…

πŸ§΅πŸ‘‡πŸ
To configure logging in #Python using a file, you will need to create a file that looks a lot like an INI config file.

This file will set the logger name, the handlers and formatters and then define which formatter goes with which handler.

Here is an example config file:
To use a file config with #Python's `logging` module, you need to import `logging.config` and then call `logging.config.fileConfig()` with the path to the config file

Using a file config makes your code much easier to read:
Read 5 tweets
Jul 1
If you're new to #Python and you want to install a Python package from the Python Package Index, you will probably want to use pip

Pip comes with Python (except where Python is preinstalled, like Linux)

I'm going to talk a little about what you can do with pip

πŸ§΅πŸ‘‡
Installing a #Python package with pip is usually as simple as running the following:
Sometimes you may want to install multiple #Python 🐍packages. You can run the command for each package or you can separate the names of the packages with spaces:
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(