Basic phyton programming
Welcome! I'm thrilled to help you start your Python programming journey today.
Before we dive in, I'd love to know a bit more about you so I can tailor everything to your needs. What is your main goal for learning Python (for example: automation, data analysis, web development, or just general curiosity), and do you have any prior programming experience?
I'm a complete beginner looking to automate boring tasks.
That is a perfect goal! Automating those tedious, repetitive tasks is exactly where Python shines.
Before we build our structured learning plan and write your very first line of code, I have one quick question to make sure we make this as relevant to your daily life as possible.
What specific repetitive tasks are you hoping to automate first (for example: renaming files, working with spreadsheets, sending emails, or scraping data from websites)?
Working with Excel spreadsheets
Excel automation is a brilliant goal for Python! We'll start from the absolute beginning, setting up a solid foundation so you can confidently write scripts to handle those spreadsheets.
print() function. It simply displays whatever text or information we tell it to inside the parentheses.
For example, if you want Python to say hello to you, we write it like this:
print("Hello, spreadsheet wizard!")
The quotes inside the parentheses let Python know that this is raw text we want to display, rather than a command or code it needs to run.Continue to variables and basic naming rules
Let's move right into variables and how to name them! Think of a variable as a labeled storage box inside your computer's memory. When you are automating Excel spreadsheets later on, you will use these boxes to hold onto things like file names, sheet titles, or the total number of rows you need to process.
To put something inside a box, we use the , which is just a single equal sign (=). On the left side, you write the name of your box, and on the right side, you write the value you want to store.
Let's look at a quick example of how you would store a spreadsheet file name:
excel_file = "monthly_report.xlsx"
Now, whenever you tell Python to use excel_file, it will look inside that box and see "monthly_report.xlsx".
To keep your code running smoothly, Python has a few strict rules and some friendly guidelines for naming your variables:
- No spaces allowed: If you want to use multiple words, join them with an underscore, like
total_rowsorsheet_name(this style is called snake_case). - Start with a letter: Your variable name must start with a letter or an underscore, never a number (e.g.,
1st_sheetis illegal, butsheet_1is perfectly fine). - Only letters, numbers, and underscores: You can't use special symbols like exclamation points, dashes, or $ symbols in your variable names.
- Case sensitivity:
my_fileandMy_Fileare treated as two completely different, unrelated storage boxes.
Continue to core primitive data types (integers, floats, strings, booleans)
Let's dive straight into Python's core primitive data types! Whenever you pull data out of an Excel sheet, Python needs to understand what kind of information it is looking at so it knows how to handle it.
Python has four essential that act as the building blocks for all your automation scripts. Let's look at them through the lens of data you would find in a typical Excel spreadsheet:
| Data Type | Python Name | What It Is | Excel Example |
|---|---|---|---|
| Integer | int | Whole numbers (no decimals) | row_count = 150 |
| Float | float | Numbers with decimal points | price = 19.99 |
| String | str | Text wrapped in quotes | customer = "Alice" |
| Boolean | bool | True or False values | is_processed = True |
Python is smart enough to figure out the type automatically based on how you write the value. If you write a number without a decimal, it's an int. If you add a decimal point, it instantly becomes a float.
Strings must be wrapped in quotes (either single or double) so Python knows it's text and not a variable name. Booleans must start with capital letters (True or False) without quotes.
Why does this matter? If you try to mathematically add the string "5" and the integer 5, Python will throw an error because it doesn't know how to add text to a number!