Introduction to Python

Python is a versatile and beginner-friendly programming language known for its readability and simplicity. It's widely used in various fields, including web development, data analysis, artificial intelligence, scientific computing, and automation.

Features of Python

  1. Python's syntax is designed to be readable and straightforward, which helps in writing clean and maintainable code. Its use of indentation for block structures enhances readability.

  2. Python is dynamically typed, meaning that you don’t need to declare the type of a variable explicitly. The type is determined at runtime.

  3. Python is an interpreted language, meaning code is executed line-by-line, which facilitates interactive coding and debugging.

  4. Python abstracts low-level details such as memory management and hardware interaction, making it easier to develop complex applications without dealing with hardware specifics.

  5. Python includes a vast standard library that provides modules and functions for various tasks, such as file I/O, system calls, and web development.

  6. Python code can run on different operating systems (Windows, macOS, Linux) with little to no modification.

  7. Python supports multiple programming paradigms, including object-oriented programming (OOP) and functional programming.

  8. There is a rich ecosystem of third-party libraries and frameworks available for Python, including tools for web development (Django, Flask), data analysis (Pandas, NumPy), and machine learning (TensorFlow, scikit-learn).

Comparison with other Languages

Python vs. Java

  • Syntax: Python’s syntax is simpler and more readable, while Java has a more verbose syntax.
  • Typing: Python is dynamically typed; Java is statically typed.
  • Speed: Java is generally faster due to its static typing and Just-In-Time (JIT) compilation. Python is interpreted and can be slower, though it can be optimized with tools like Cython.
  • Usage: Java is commonly used in large enterprise environments and Android app development. Python is popular for data science, scripting, and web development.

Python vs. C++

  • Syntax: Python has a more straightforward and readable syntax, while C++ is more complex with manual memory management.
  • Performance: C++ typically offers better performance and control over system resources but at the cost of increased complexity and development time.
  • Memory Management: Python manages memory automatically (garbage collection), whereas C++ requires manual memory management.
  • Usage: C++ is widely used in systems programming, game development, and applications requiring high performance. Python is favored for rapid development and ease of use in areas like web development and data analysis.

Python vs. JavaScript

  • Execution Environment: JavaScript is primarily used for web development and runs in the browser, while Python is used for server-side applications, scripting, and data analysis.
  • Typing: Python is dynamically typed; JavaScript is also dynamically typed but has a different set of features and quirks.
  • Libraries and Ecosystem: JavaScript has a rich ecosystem for web development (Node.js, React, Angular), while Python has strong libraries for scientific computing and data analysis.

Python vs. Ruby

  • Syntax: Both Python and Ruby emphasize readability, but Python is known for its simplicity, while Ruby focuses more on developer happiness and flexibility.
  • Performance: Python and Ruby have similar performance characteristics, though Python often has more mature libraries for performance-critical applications.
  • Usage: Ruby is commonly associated with web development (especially with the Ruby on Rails framework), while Python has a broader range of applications including data science and automation.

Identifiers in Python

Identifiers are names used to identify a variable, function, class, module, or other objects in Python. They follow certain rules:

  • They must start with a letter (a-z, A-Z) or an underscore (_).
  • They can be followed by letters, digits (0-9), or underscores.
  • They are case-sensitive, meaning variable , Variable , and VARIABLE are different identifiers.
  • They cannot be Python keywords (reserved words).

Examples:

my_variable = 10  # Valid identifier
_age = 25         # Valid identifier (starts with an underscore)
totalAmount = 100  # Valid identifier (camelCase naming convention)
 
# Invalid identifiers
2variable = 10    # Starts with a digit, which is not allowed
my-variable = 5   # Contains a hyphen, which is not allowed
class = 'Python'  # 'class' is a reserved keyword

Keywords in Python

Keywords are reserved words in Python that have special meaning and cannot be used as identifiers. They define the syntax and structure of the Python language. Python's keywords include if , else , while , for , try , except , class , def , and more.

Here is list of all keywords in Python:

FalseNoneTrueand
asassertasyncawait
breakclasscontinuedef
delelifelseexcept
finallyforfromglobal
ifimportinis
lambdanonlocalnotor
passraisereturntry
whilewithyield

Statements and Expressions

Statements are instructions that perform an action. They include assignments, loops, conditionals, and function definitions. Each statement performs an operation and ends with a newline or a semicolon.

Examples of Statements:

statements.py
x = 10           # Assignment statement
if x > 5:        # Conditional statement
    print("x is greater than 5")
 
for i in range(5):  # Loop statement
    print(i)

Output:

x is greater than 5
0
1
2
3
4

Expressions are combinations of values, variables, operators, and function calls that are evaluated to produce a value. Expressions do not perform actions but rather compute values.

Examples of Expressions:

a = 5 + 3        # '5 + 3' is an expression that evaluates to 8
b = a * 2        # 'a * 2' is an expression that evaluates to 16
result = (a + b) / 2  # '(a + b) / 2' is an expression that evaluates to 12

Variables in Python

In Python, variables are used to store data that can be referenced and manipulated in a program. They are created by assigning a value to a name. Python uses dynamic typing, so you don't need to declare the type of the variable explicitly. They are flexible and can hold different types of data, allowing for dynamic and versatile programming.

A variable name in Python must follow these rules:

  1. Must Start with a Letter or Underscore: A variable name must begin with a letter (a-z, A-Z) or an underscore (_).
  2. Can Contain Letters, Digits, and Underscores: After the first character, the name can contain letters, digits (0-9), and underscores.
  3. Cannot Start with a Digit: Variable names cannot begin with a number.
  4. Case-Sensitive: Variable names are case-sensitive. For example, myVariable, MyVariable, and MYVARIABLE are considered different variables.
  5. Cannot Be a Reserved Keyword: Variable names cannot be one of Python's reserved keywords.

Examples of Legal Variable Names:

variable1 = 10           # Valid
_variable = 20           # Valid
varName = 30             # Valid
var_name = 40            # Valid

Examples of Illegal Variable Names:

1variable = 10          # Invalid (starts with a digit)
var-name = 20           # Invalid (contains a hyphen)
class = 30              # Invalid (keyword)

Assigning Values to Variables

In Python, assigning a value to a variable is done using the = operator. The value on the right side of the = operator is assigned to the variable on the left side. Python is dynamically typed, so the type of the variable is determined at runtime based on the value assigned to it.

Basic Assignment:

x = 5                  # Assigns integer value 5 to variable x
y = 3.14               # Assigns floating-point value 3.14 to variable y
name = "Eniv"          # Assigns string value "Eniv" to variable name

Multiple Assignments: You can assign the same value to multiple variables at once:

a = b = c = 100        # Assigns the value 100 to variables a, b, and c

You can also assign different values to multiple variables in a single statement:

x, y, z = 1, 2, 3      # Assigns 1 to x, 2 to y, and 3 to z

Variable Reassignment: Variables in Python can be reassigned to different values of any type:

x = 10                # Initially x is an integer
x = "Hello"           # Now x is a string

Swapping Values: Python allows you to swap values between variables easily:

a = 5
b = 10
a, b = b, a           # Swaps the values of a and b
# Now a is 10 and b is 5

Example:

variables.py
# Example 1: Basic assignment
x = 5
print(x)
 
# Example 2: Multiple assignments
a, b = 10, 20
print(a, b)
 
# Example 3: Reassignment
x = 100
print(x)
x = "New value"
print(x)
 
# Example 4: Swapping values
x, y = 1, 2
x, y = y, x
print(x, y)

Output:

5
10 20
100
New value
2 1
How's article quality?

Last updated on -

Page Contents