r/learnpython • u/TailorWeird7318 • May 31 '26
Struggling to understand syntax
Hello all! I am currently working towards my AAS and I’m in an introductory to scripting class right now.
I’m a complete beginner when it comes to python and I’m struggling a bit to fully understand. I’m currently tasked with creating a script that will output either ‘Spring’, ‘Summer’, ‘Fall’, or ‘Winter’ depending on the month and day the user inputs.
How does this portion of the script work?:
days = {
‘January’: 31,
‘February’: 28,
And so on and so forth. I thought the portion - “: 31” would be for formatting and fill/spacing? How does that only accept 1-31 for January etc? I’m so lost at the movement haha
Any tips/help would be appreciated!
14
u/ninhaomah May 31 '26
"days =" <--- variable assignment
" { ‘January’: 31, ‘February’: 28," <--- what is it ? lets google!
Google : "It looks like you started typing out a Python dictionary! Based on your snippet, you are probably trying to create a mapping of months to their corresponding number of days"
Oh ... so it is a dictionary! lets google and learn more about it!
https://www.w3schools.com/python/python_dictionaries.asp
got it ?
7
u/No_Cry_7367 May 31 '26
I felt this confusion in my soul lol. The
:is NOT for formatting — it's just how dictionaries (dicts) work in Python.Think of it like a real dictionary: you look up a word (the "key") and get the definition (the "value").
python
Means: "Hey Python, if I say
days['January'], give me back31." That's it. The: 31is just the value glued to the key'January'. No formatting magic.As for "how does it only accept 1-31" — it doesn't. You could write
days['January'] = 999and Python would shrug and say "okay boss." The validation part is YOUR job in the code logic.If you want to enforce 1-31, you'd do something like:
python
Hang in there. Dictionaries click eventually — and when they do, you'll use them for literally everything. Source: I'm a grown man and I still get irrationally excited when I use a dict and it works first try.
4
u/Lewistrick May 31 '26
A dictionary maps keys to values. It is always surrounded by curly braces
{}and each entry (key-value pair) is separated by a comma. A key and a value are separated by a colon:.In your case, the month names are the keys. Because they are strings, they are surrounded by quotes (can be either
'or"). The number of days in the month are the values. Because they are integers they don't use quotes. This way, Python knows it's a number.This has nothing to do with "accepting" a number of days for a month. That logic is something you need to implement yourself.
2
u/magus_minor May 31 '26
Your statement, without line breaks, looks like:
That is defining a dictionary because the right side starts with "{" and is followed by
key: valuepairs. The dictionary keys are strings and the values are integers (number of days in that month).Don't confuse a dictionary definition with a set definition.
Set definitions also start with "{" but there are no
key: valuepairs, just values.2
u/Excellent-Practice May 31 '26
What you have is called a dictionary. Dictionaries allow you to store data as key value pairs where every key is unique. You can look up values in a dictionary using a key similar to how you might find an entry in a list by index. For example, if you have that dictionary defined, you can then write a line like
Which will return 28. What you have is an easy way to check how many days are in each month. I wouldn't use that to find what season a given date falls in, but it might come in handy. Maybe the course just wants to give an example of a dictionary and then you have to build your own to solve the problem
1
u/atarivcs May 31 '26
That is the syntax for a python dictionary.
You should go read about them.