Python Syntax

JK1852 
Created at Jun 13, 2023 08:01:26
Updated at May 10, 2024 07:27:51 
  6,692   0   0  

Execute Python Syntax

As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line:

>>> print("Hello, World!")
Hello, World!

Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:

C:\Users\Your Name>python myfile.py

Python Indentation

Indentation refers to the spaces at the beginning of a code line.

Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.

Python uses indentation to indicate a block of code.

if 5 > 2:
  print("Five is greater than two!")

Below Python code will give you an error, because you skip the indentation:

if 5 > 2:
print("Five is greater than two!")

The number of spaces is up to you as a programmer, the most common use is four, but it has to be at least one.

if 5 > 2:
 print("Five is greater than two!") 
if 5 > 2:
        print("Five is greater than two!") 

You have to use the same number of spaces in the same block of code, otherwise Python will give you an error:

if 5 > 2:
 print("Five is greater than two!")
        print("Five is greater than two!")

Python Variables

In Python, variables are created when you assign a value to it:

x = 5
y = "Hello, World!"

Comments

Python has commenting capability for the purpose of in-code documentation.

Comments start with a #, and Python will render the rest of the line as a comment:

#This is a comment.
print("Hello, World!")

 



Tags: Python Python Comments Python Syntax Python Variables Share on Facebook Share on X

◀ PREVIOUS
Python Getting Started

▶ NEXT
Python Comments

  Comments 0
SIMILAR POSTS

Python Comments

(updated at May 10, 2024)

Python Variables

(updated at May 10, 2024)

Python Getting Started

(updated at May 10, 2024)

Python Introduction

(created at Jun 12, 2023)

What is Python?

(updated at Jan 04, 2024)

Python Data Types

(updated at May 10, 2024)

Python String Operations

(updated at May 10, 2024)

Python Lists

(updated at May 10, 2024)

String concatenation by join()

(created at Jun 14, 2023)

Printing string n times

(created at Jun 14, 2023)

Python Arithmetic Operators

(created at Jun 14, 2023)

Python Comparison Operators

(updated at May 10, 2024)

Python Tuples

(updated at May 09, 2024)

Python Sets

(updated at May 09, 2024)

Python Dictionaries

(updated at May 09, 2024)

Python Conditions and If statements

(updated at May 09, 2024)

Python While Loops/For Loops

(updated at May 09, 2024)

Python Functions

(updated at May 09, 2024)

Python Lambda

(updated at May 09, 2024)

Python Arrays

(updated at May 09, 2024)

Python Classes/Objects

(updated at May 09, 2024)

Python Inheritance

(updated at May 09, 2024)

Python Iterators

(updated at May 09, 2024)

Python Polymorphism

(updated at May 09, 2024)

Python Scope

(updated at May 09, 2024)

Python Modules

(updated at May 09, 2024)

Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering

(updated at Sep 23, 2024)

Machine Learning Types and Programming Languages

(updated at Nov 29, 2023)

Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship

(updated at May 10, 2024)

Mastering Excel Data Manipulation with Python

(updated at Apr 26, 2024)

Challenge: One Code Problem Per Day

(created at Oct 03, 2025)

OTHER POSTS IN THE SAME CATEGORY

Python Functions

(updated at May 09, 2024)

Python While Loops/For Loops

(updated at May 09, 2024)

Python Conditions and If statements

(updated at May 09, 2024)

Python Dictionaries

(updated at May 09, 2024)

Python Sets

(updated at May 09, 2024)

Python Tuples

(updated at May 09, 2024)

Python Comparison Operators

(updated at May 10, 2024)

Python Arithmetic Operators

(created at Jun 14, 2023)

Printing string n times

(created at Jun 14, 2023)

String concatenation by join()

(created at Jun 14, 2023)

Python Lists

(updated at May 10, 2024)

Python String Operations

(updated at May 10, 2024)

Python Data Types

(updated at May 10, 2024)

Python Variables

(updated at May 10, 2024)

Python Comments

(updated at May 10, 2024)

Python Getting Started

(updated at May 10, 2024)

Python Introduction

(created at Jun 12, 2023)

What is Python?

(updated at Jan 04, 2024)

UPDATES

LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...

(created at Oct 18, 2025)

LEE CHANHYUK - Endangered Love (멸종위기사랑)

(created at Oct 18, 2025)

Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)

(created at Oct 18, 2025)

Common methods to improve coding skills

(created at Oct 18, 2025)

US National Holiday in 2026

(created at Oct 18, 2025)

BABYMONSTER “WE GO UP” Band LIVE [it's Live] K-POP live music show

(created at Oct 18, 2025)

BLACKPINK - ‘Shut Down’ Live at Coachella 2023

(created at Oct 18, 2025)

JENNIE - like JENNIE - One of Hot K-POP in 2025

(created at Oct 18, 2025)

BABYMONSTER(베이비몬스터) - DRIP + HOT SOURCE + SHEESH

(created at Oct 08, 2025)

Common Naming Format in Software Development

(created at Oct 07, 2025)

In a life where I don't want to spill even a single sip of champagne - LEE CHANHYUK - Panorama(파노라마)

(created at Oct 06, 2025)

Countries with more males and females - what about UIUC?

(created at Oct 04, 2025)

Challenge: One Code Problem Per Day

(created at Oct 03, 2025)

Urban planning and growth from a historical perspective

(created at Sep 28, 2025)

Jackbryan VS Serpent | Korea Beatbox Championship 2023 | Quarterfinal

(created at Sep 28, 2025)

CNBLUE - You've Fallen For Me (넌 내게 반했어)

(created at Sep 28, 2025)

GGIS: The Roots of Visualizing Geographic Information

(created at Sep 27, 2025)

CNBLUE - 외톨이야 (aka Outsider)

(created at Sep 27, 2025)

Did you know that the person who voiced Humtrix Rumi in KPop Demon Hunters went to UIUC?

(updated at Sep 05, 2025)

WING - Dopamine

(created at Sep 05, 2025)

CARDIO VS Jackbryan | Korea Beatbox Championship 2025 | Semifinal

(updated at Sep 04, 2025)

Tech Visionaries who graduated at UIUC - You are the Next Turn

(updated at Sep 04, 2025)

Thinking about the Public Dataset and Open API provided for the Authorized People

(updated at Sep 04, 2025)

Where to Eat with Your i-Card at UIUC and How to Track Your Dining Dollars

(updated at Sep 04, 2025)

OMG! Did you consume your meals already at UIUC? How do you change the meal plan?

(updated at Sep 03, 2025)

Java Comments

(updated at Sep 03, 2025)

Abraham Lincoln Contributed to UIUC's Creation and its Mission

(created at Sep 03, 2025)

Feeling weak? Transform yourself at the UIUC ARC!

(updated at Sep 03, 2025)

Checking Your Upcoming Assignment/Exam Schedule: Using the UIUC Canvas Dashboard for Assignment Management

(updated at Sep 03, 2025)

UIUC Course Map for CS and Blended CS Degrees

(updated at Sep 02, 2025)

What You Need to Prepare for Graduate University at UIUC

(updated at Sep 01, 2025)

Did you know about the UIUC Course Numbering Policy? How to meet with 120 GPA hours?

(created at Sep 01, 2025)

My Dad's Bucket Hat Craze: One Man's Quest for Collegiate Headwear

(created at Aug 30, 2025)

Public Transportation between Chicago O'Hare International Airport and UIUC (University of Illinois at Urbana-Champaign)

(updated at Aug 27, 2025)

How to Receive Mail and Packages in University Housing at UIUC

(updated at Aug 27, 2025)

When you are too busy to have your breakfast/lunch/dinner, use Good2Go Carryout Program

(created at Aug 27, 2025)

Why Outlook’s Redirection Option Is a Game-Changer

(updated at Aug 27, 2025)

Why Every Freshman Needs the Illinois App at UIUC

(updated at Aug 24, 2025)

My First Day at University of Illinois-Urvana Champaign

(created at Aug 22, 2025)

Did you get Selective Service System(SSS) Form 3C?

(updated at Aug 17, 2025)

BLACKPINK's refreshing song - Jump

(updated at Aug 08, 2025)

Poisonous Mushrooms sprouted along the roadside after Typhoon

(updated at Aug 06, 2025)

Annual Weather Forecasting in Illinois based on Month

(updated at Aug 06, 2025)

My name has a typo in MyIllini - Need a Biographical change form

(updated at Jul 31, 2025)

Free Transportation Systems for UIUC students, faculty, and staff with I-Card

(updated at Jul 31, 2025)

What you can do with I-Card at UIUC

(updated at Jul 31, 2025)

Selecting a Bed Configuration before you move-in at UIUC Dormitory

(updated at Jul 30, 2025)

Student Health Insurance Waiver: Major Deadlines You Can’t Miss

(updated at Jul 22, 2025)

Types of Memory and Storage

(updated at Jul 22, 2025)

Sample Curriculum Comparison CS versus CS+GGIS at UIUC

(updated at May 31, 2025)