Sunday, April 20, 2025

Python Syntax, Indentation

In programming, we can create "containers". Official name for some specific "container" is variable.

Variables can contain numbers, words, characters, whole sentences, and so on. Also, we can override things in variables with new things.

Let's create a variable "something", so we can put some data there, for example: "Some String Here".

After that, we will immediately use it - just simple printing, nothing special here:


something = "Some String Here"

print(something)

Result:


Some String Here
>>> 

Python doesn't care about spaces inside parentheses (when we use variables):


print(  something  )

print(      something      )

But space inside quotes is extremely important when we do direct printing. Everything inside quotes is part of usable string. Type and run this code:


print("some string")
print(" some string ")
print("      some string     ")
print(   "      some string     "    )
print(      "some string"    )

Result:


some string
 some string 
      some string     
      some string     
some string
>>> 

Try this to see what's happening:


print("string with more parts")
print("string     with     big     spaces")

In Python, indentation is extremely important. What you see is "if statement". We will use it all the time in programming.


if 5 < 10:
    print("This Will Work")

But this code will not work - because we messed up indentation rules:


if 5 < 10:
print("This Will NOT Work")

So far so good. In next tutorial we will talk about internal code explanations (comments and docstrings).

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...