Skip to main content

Python Syntax

 Python Syntax

Python syntax refers to the set of rules that define how a Python program is constructed. At its core, syntax encompasses everything from the structure of statements and expressions to the way comments are used and how blocks of code are organized. Python's syntax is renowned for its clarity and simplicity, making it an ideal choice for beginners in programming. Unlike many other languages, Python emphasizes readability, which reduces the cost of program maintenance and development. This emphasis on readability is achieved through the use of whitespace to define code blocks and the avoidance of unnecessary symbols, making Python code almost resemble pseudocode.

Execute First Python Code


Python Syntax

print() function: This is a built-in Python function that outputs the specified message to the screen.

"Hello World": This is a string, a type of data in Python that represents text. In this case, the text is Hello World.

How it Works:

When you run this statement, Python executes the print() function with "Hello World" as its argument. An argument is a value that you pass to a function to tell it what to do. In this case, you're telling print() to display "Hello World".

Comments

In Python programming, comments are used to include explanations or annotations within the code that are not executed by the interpreter. They serve to enhance code readability, provide context, and document the purpose of various sections of code.

Python Single Line Comment

In programming, when you see a line of code with a "#" symbol before it, it means that anything written after the "#" symbol is just a note for humans to read, not for the computer to run. It's like writing a reminder or explanation to yourself or other programmers. The computer ignores everything after the "#" symbol on that line. This helps us understand the code better without affecting how the code works when it runs.
 
Python Syntax

Python Multi-line Comment

In Python, there isn't a special way to write comments that span multiple lines directly. Instead, programmers often use several single-line comments, one after another, or they use triple quotes (either ''' or """), even though these are technically used for writing strings, not comments. This helps programmers add comments over several lines to explain their code.

Python Syntax

Python Variables

Python variables are like containers that hold information or data. Imagine a box where you can put things like numbers, words, or even complex pieces of information. You can give these boxes names, and that's what variables are for—they have names that you can use to refer to the data inside them. For example, you can have a variable called "age" that holds the number 25, or a variable called "name" that holds the word "John". Variables help keep track of data in your program and allow you to work with and manipulate that data easily without having to remember or retype it each time you need it.Python Syntax

Python Keywords Words

Python keywords also known as reserved words, are special words that have predefined meanings and cannot be used as identifiers (such as variable names or function names) in Python code. These reserved words are part of the Python language syntax and serve specific purposes. It's important to avoid using these Keywords words as variable names, function name or a class name to prevent conflicts and ensure the proper functioning of your Python programs. Given below are the Keywords/ Reserved words of Python 3.9.

Keywords
FalseNoneTrueandas
assertasyncawaitbreakclass
continuedefdelelifelse
exceptfinallyforfromglobal
ifimportinislambda
nonlocalnotorpassraise
returntrywhilewithyield



Comments