StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover Basic syntax
Download
  1. Python
  2. Unlocking Python: Foundations for Coding
Installing Python: A Step-by-Step Guide : Interactive shell
Unlocking Python: Foundations for Coding

Python Basic Syntax

Python's syntax is designed to be clean and readable, making it a great language for beginners. Let's dive into some fundamental elements:

Indentation

  • Python uses whitespace indentation to define code blocks.

  • It's crucial for code structure and readability.

  • Consistent indentation (usually 4 spaces) is essential.

Python

if condition:

    # This code block belongs to the if statement

    print("Condition is true")

else:

    # This code block belongs to the else statement

    print("Condition is false")

Comments

  • Use # for single-line comments.

  • Use triple quotes (""" or ''' ) for multi-line comments.

Python

# This is a single-line comment

print("Hello, world!")

 

"""

This is a multi-line comment.

It can span multiple lines.

"""

Variables

  • No need to declare data types explicitly.

  • Use = for assignment.

Python

message = "Hello"

number = 42

Data Types

  • Numbers: Integers (e.g., 42), floating-point numbers (e.g., 3.14).

  • Strings: Text enclosed in single (') or double (") quotes.

  • Booleans: True or False values.

  • Lists: Ordered collections of items (mutable).

  • Tuples: Ordered collections of items (immutable).

  • Dictionaries: Unordered collections of key-value pairs.

Operators

  • Arithmetic: +, -, *, /, //, %, **

  • Comparison: ==, !=, <, >, <=, >=

  • Logical: and, or, not

Control Flow

  • if-else: Conditional execution.

  • for: Iterating over sequences.

  • while: Repeating a block of code while a condition is true.

Python

if x > 0:

    print("x is positive")

else:

    print("x is non-positive")

 

for i in range(5):

    print(i)

 

while count < 10:

    count += 1

Functions

  • Define reusable code blocks.

  • Use def keyword.

Python

def greet(name):

    print("Hello,", name)

 

greet("Alice")

Input and Output

  • print() for output.

  • input() for user input.

Python

name = input("Enter your name: ")

print("Hello,", name)

Basic Examples

Python

# Simple calculation

result = 5 + 3 * 2

print(result)

 

# List of fruits

fruits = ["apple", "banana", "orange"]

for fruit in fruits:

    print(fruit)

Remember: Consistent indentation, clear variable names, and comments enhance code readability.

 

Installing Python: A Step-by-Step Guide Interactive shell
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website