Python Basics

Python Basics

What are basics and fundamentals of programming in Python?

ยท

5 min read

Here is a summary of the basics of Python:

  • Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.

  • It has a simple and easy-to-learn syntax. Some key syntax elements:

    • Uses indentation, rather than braces {} to delimit blocks (code blocks).

    • Uses the keyword 'def' to define functions.

    • Uses the keyword 'class' to define classes.

  • Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles.

  • The core data types in Python include numbers, lists, tuples, dictionaries, sets and strings.

  • Python has a large standard library that provides useful modules for string manipulation, OS interaction, network programming, etc.

  • Python code is executed by the Python interpreter. The code can be run interactively from the command line or using scripts.

  • Python supports OOP features like inheritance, polymorphism and encapsulation.

  • It also has exception handling, generators, lambda functions and many other advanced features.


Fundamentals

Here are the fundamentals of Python programming:

  1. Variables - Variables store values that can be used throughout the program. Variables are created when you assign a value to them. Python has no command for declaring a variable.

  2. Data Types - Python has several built-in data types like int, float, bool, string, list, tuple, dict, set etc.

  3. Expressions and Operators - Python supports arithmetic operators (+ - * /), comparison operators (== != > <), assignment(=), logical operators (and or not) etc.

  4. Keywords - Words like import, class, def etc. have special meanings in Python.

  5. Indentation - Code within every block (if/else, for, def etc) must be indented usually with 4 spaces.

  6. Functions - Functions allow you to split your code into modular reusable parts. Defined using the def keyword.

  7. Classes - Used to model real-world objects. Defined using the class keyword.

  8. Loops - for and while loops are used for iteration.

  9. Modules - Modules allow you to organize your code and reuse it across programs. Imported using the import statement.

  10. Exception Handling - Using try/except blocks to catch and handle exceptions.


Data types

The main data types in Python are:

  1. Numbers:
  • int - Examples: 1, 2, 3, -5

  • float - Examples: 1.5, 2.7, -4.2

  • complex - Examples: 1 + 2j, 5j

  1. String - Any text within quotes:
  • 'Hello'

  • "Hello"

  1. List - Ordered collection of items:
  • [1, 2, 3]

  • ['a', 'b', 'c']

  1. Tuple - Immutable ordered collection:
  • (1,2,3)

  • ('a', 'b', 'c')

  1. Set - Unordered collection of unique items:
  • {1, 2, 3}

  • {'a', 'b', 'c'}

  1. Dictionary - Unordered collection of key-value pairs:
  • {'a': 1, 'b': 2}

  • {1: 'a', 2: 'b'}

  1. Boolean - Represents one of two values: True or False

In addition, Python has built-in types like:

  • bytes

  • bytearray

  • range

You can also define your own classes which become custom data types.

So in summary, Python has a rich set of built-in data types to represent various types of data in your programs.


Dynamic language

Python is a dynamically typed language, which means:

  • Variables do not have fixed types. A variable can hold different types of data during the runtime of a program.

Advantages:

  • Flexible - Variables can change types freely during runtime. This allows for rapid prototyping and easy refactoring.

  • Concise - There is no need to declare variable types before use. The code is more readable.

Disadvantages:

  • Error prone - Since types are checked at runtime, type errors may only show up late during execution. This can make debugging harder.

  • Inefficient - The interpreter needs to check and convert types at runtime. This can impact performance.

On the other hand, statically typed languages like Java or C# require you to declare the type of variables before use. This has the following advantages:

  • Robust - Type errors are caught at compile time, making the code less error prone.

  • Efficient - The compiler can optimize the code based on type information.

So in summary:

  • Dynamic typing makes Python very flexible and concise. But it can also make the code error prone and less efficient.

  • Static typing makes languages like Java and C# more robust and efficient. But it requires more type declarations, making the code less concise.

It's a trade-off between flexibility vs robustness. Ultimately it depends on the type of application you are building and your personal preferences. Both approaches have their merits.


Type inference

Type inference is a feature of dynamically typed languages like Python where the interpreter implicitly determines the type of an object, without an explicit type declaration.

For example:

a = 10

Here, we assigned an integer literal 10 to the variable a. The interpreter implicitly determines that a refers to an integer, without us explicitly declaring its type.

This works for other types as well:

s = "Hello" # s is a string
l = [1, 2, 3] # l is a list

The interpreter determines that:

  • s refers to a string

  • l refers to a list

based on the initial assignment.

This is in contrast to statically typed languages like Java where you have to explicitly declare the type of a variable:

int a = 10;
String s = "Hello"; 
List l = new ArrayList();

Here you have to declare a as an int, s as a String and l as a List.

The benefits of type inference are:

  • Makes Python code more concise since there are no type declarations

  • Allows variables to change types freely during runtime

However, since types are only checked at runtime, type errors may only show up late during execution.

In summary, type inference allows the Python interpreter to implicitly determine the type of a variable based on its initial assignment, without requiring an explicit type declaration. This makes Python code more concise, but can also make it less robust.


Disclaim: This entire article was created by Rix. I have asked the relevant questions in order and posted the responses. Follow up this series to the end and when you finish you will know python. Learn and prosper. ๐Ÿ––

ย