DEV Community

Kelvin
Kelvin

Posted on

Introduction to Python

Python is a powerful and flexible programming language created by Guido van Rossum and first released in 1991.Over the years, Python has become one of the most loved and used languages in the world.

Python installation.

python can be installed from Microsoft store or the browser.

python installation from the web.
python then select version.

checking the version of python installed in your pc

Open your command line and type python --version

This confirms the version of python installed in your computer.

example of a python code.

What Can You Use Python For

  • Web Development, and software development.
  • Data analysis, machine learning, data engineering, AI, and math.
  • System scripting.

Why Use Python?

  • Platform-independent - Python works everywhere: Windows, Mac, Linux, Raspberry Pi, or any other platform.
  • Simple & Readable Syntax - python is simple to read and understand.
  • Fewer Lines of Code - Python lets you do more with less typing.
  • Fast Prototyping - Python runs your code line by line, immediately showing results or errors.
  • Multiple Programming Styles - Python lets you write code in different ways i.e. procedural, object oriented and functional (using functions like math operations)

Python Syntax.

Python has rules about how code should be written so it can be understood and executed properly. It relies on indentation and clean line-by-line execution and throws errors if the rules are not followed. Python emphasizes readability and simplicity, making it easier for beginners to learn and professionals to maintain.

Key Syntax Rules in Python.

Python uses indentation to define blocks of code.
Indentation means adding spaces at the beginning of a line to show that this line belongs to a group or block of code.
You can technically use any number of spaces - at least one. But the convention is to use four spaces for each indentation level.

if 5 > 2: 
    print("Five is greater than two!") 
Enter fullscreen mode Exit fullscreen mode

Mixing indentation levels is not allowed Python needs consistency. Once you choose a number of spaces, stick with it throughout the block.

Python Variables.

A variable is a named memory location.

name = "lee"
    print("name")
Enter fullscreen mode Exit fullscreen mode

here name is a variable for storing the string "lee"

In Python, you don’t have to declare the data type of a variable like you do in many other languages. Python understands it based on the value you assign.

Rules of naming variables.

  • A variable name must start with a letter (a–z, A–Z) or an underscore _.
  • It can only contain alphanumeric characters and underscores (A–Z, a–z, 0–9, and _)
  • Variable names are case-sensitive.
  • It cannot start with a number.
  • You cannot use reserved words i.e. (int, True ETC)

Best Practices.

  • Use descriptive names
  • Use snake_case for variables
  • Avoid short, vague names like x, y, unless in quick examples or math

Assigning Multiple Variables

Python lets you assign values to multiple variables at once: a, b, c = 1, 2, 3
All at once! Or assign the same value to multiple variables: x = y = z = "Python"

Checking the Type of a Variable - You can use the type() function to find out what kind of value a variable holds.

name = "Alice" 
print(type(name)) 
Enter fullscreen mode Exit fullscreen mode

the output will be str (string)

Python Constants.

  • Values that don’t change during normal program execution
  • In real life, think of your birthdate. You store it once and it never changes - that's a constant.
  • In Python, we use uppercase letters to indicate that a variable is meant to be a constant e.g.
PI = 3.14159
Enter fullscreen mode Exit fullscreen mode
  • Python does not have built-in constants, but we use uppercase variable names to show they are constants.

Python Data Types.

A data type is the classification of data that tells the computer what kind of data you are working with and what operations can be performed on it.

Python’s Built-in Data Types.

1. Numeric Types

  • int – Integer (whole numbers)
age = 25
Enter fullscreen mode Exit fullscreen mode
  • float – Floating point (decimal numbers)
height = 5.9
Enter fullscreen mode Exit fullscreen mode
  • complex – Complex numbers (used in scientific computing)
complex_num = 3 + 2j 
Enter fullscreen mode Exit fullscreen mode

2. Text Type

  • str – String (a sequence of characters)
"Python is amazing!"
Enter fullscreen mode Exit fullscreen mode

3. Boolean Type

  • bool - Can only be True or False.
is_logged_in = True
Enter fullscreen mode Exit fullscreen mode

Arithmetic Operators in Python

  • Addition (+) -- Adds two numbers
  • Subtraction (-) -- Subtract right from left
  • Multiplication (*) -- Multiply numbers
  • Division (/) -- Divide and get float
  • Modulus (%) -- Get the remainder

comparison operators

Greater than (>)
Less than (<)
Equal to (==) note that (=) is an assignment operator
Not equal to (=!)
Greater than or equal to (>=)
Less than or equal to (>=)

Logical operators (and,or,not)

AND - Returns true only if both conditions are true
OR - Returns true if at least one condition is true
NOT - Reverses the value (true becomes false, false becomes true)

Top comments (0)