Skip to main content

Command Palette

Search for a command to run...

Python

Updated
4 min readView as Markdown
Python

Python is a general-purpose high-level programming language that allows programmers to focus more on problem-solving than syntax errors. It is an easy-to-learn and versatile language, widely adopted by non-programmers such as scientists and finance professionals. The reason behind the popularity of Python for data science is that:

  • Python offers an extensive ecosystem of robust frameworks and libraries such as Scikit-learn, Sci-py, Numpy, Pandas, and Matplotlib.

  • Python has a proximity to Maths that provides efficient data manipulation capabilities, machine learning algorithms, and visualization capabilities.

  • Python offers the capability to perform various tasks in data science workflows such as preprocessing, exploratory data analysis, modeling, and visualization.

Python Output

To print any output to a stream, Python provides a built-in function print. We can also change the settings of the function to get the desired format.

print("Maham",7,9.1,True)
print("Maham",7,True,sep='/') # sep will put '/' between the values

Data Types

Python can handle different data types, built-in by default.

  • Numeric types: int, float, complex

  • Text type: str

  • Sequence types: list, tuple, range

  • Mapping type: dict

  • Set types: set, frozenset

  • Boolean type: bool

  • Binary types: bytes, bytearray, memoryview

  • None type: NoneType

We can get the type of data using a built-in type() function :

# Print the data type (float in this case)
print(type(2.9))

Variables

Variables are containers that can refer to the objects in memory. Unlike other programming languages, Python is a dynamically typed language. Dynamic typing means that type of the variable is determined at runtime by the interpreter ( e.g., a=9, b='Malta' ). This behavior is achieved through dynamic binding.

Dynamic Binding:

Dynamic binding refers to the ability of a variable to be associated with different objects at run time. This means that the value and type of the variable can change dynamically during the execution of the program. Suppose we have a variable called name

name = 'Sumar' # name is variable referencing to the object Sumar
print(name) # Output: Sumar

name = 10 # Now variable name is referencing to the object 10 
print(name) # Output: 10

Keywords and identifiers:

In Python, there are predefined reserved words that have specific functionalities within the language.These keywords are integral to the syntax and semantics of Python and are used to define data types, operators, control structures and other elements of the language. It should be noted that keywords should not be used as variable names to prevent conflicts. To check the keywords in Python;

import keyword
print(keyword.kwlist)

An identifier is a name used to identify a variable, function,class, module or other entities within the code. An identifier acts as a unique label that represents a specific element in the program.

Rules for defining identifiers:

  • Identifiers are case-sensitive, for instance, *'*myName' and *'*myname' are considered different identifiers.

  • An identifier can be a combination of letters (both lowercase and uppercase), digits and underscore.

  • The first character of an identifier must be a letter (a-z, A-Z) or an underscore (_)

  • It is a common practice to use lowercase letters with words separated by underscores (snake_case) for variable and function names and capitalize the initial of each word (CamelCase) for class names.

User Input

Users can provide input to the program using the input function. The default type of the input function is a string because it is the universal format for data storage (i.e. string can store integers and floats but vice versa is not possible). However, it is possible to change the type of input by explicitly specifying the desired data type.

user_name = input('Enter your name :') # type of user_name is string
user_age = int (input('Enter your age :')) # type of the age will change to integer
user_weight = float (input('Enter your weight:')) # the input will change to float

Operators in Python

Operators are the symbols that represent specific operations on operands. Python provides a variety of operators that can be classified into different categories.

  1. Arithmetic operators ( +, -, /, *, //, ** )

  2. Relational operators ( > ,< ,>= ,<= ,== , != )

  3. Logical operators ( and , or , not )

  4. Bitwise operators ( & , | , ^ , ~, >> , << )

  5. Assignment operators ( = , += , -= , %= , /= ,**= , //= )

  6. Membership operators ( in , not in )

  7. Identity operators ( is , is not)

More from this blog

Data Dive with Maham

6 posts