Coefficient of Thermal Expansion and their Importance.pptx
Programming in Civil Engineering_UNIT 2_NOTES
1. Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
UNIT II
DATA TYPES AND VARIABLES IN PYTHON
Introduction data types and variables, types of Data types and variables
Python is a dynamically typed language, which means you don't have to declare the type of a
variable when you create one. This flexibility allows Python to be very user-friendly and easy
to work with. Below, we delve into the details of data types and variables in Python.
2.1 Introduction to Data Types and Variables
Variables in Python are more than just names bound to objects. They serve as references to
objects stored in memory. Unlike some other programming languages, Python does not require
explicit declaration to reserve memory space. The declaration happens automatically when you
assign a value to a variable. The equal sign (=) is used to assign values to variables.
Dynamic Typing
Python is dynamically typed, which means the type of variable is determined at runtime, not in
advance. This provides flexibility but requires understanding how types can change.
Variable Naming Conventions
• Names can start with a letter or an underscore, not with a number.
• Names can contain letters, numbers, and underscores.
• Python is case-sensitive; thus, Variable is different from variable.
Variables are essentially the names you give to computer memory locations which are used to
store values in a programming language. In Python, variables are created the moment you
assign a value to them.
Data Types are an important concept in programming. They are the classification or
categorization of data items. They represent the kind of value that tells what operations can be
performed on a particular data. Since everything is an object in Python programming, data
types are actually classes and variables are instance (object) of these classes.
2.2 Types of Data Types in Python
Python has various standard data types that are used to define the operations possible on them
and the storage method for each of them. Python data types can be broadly classified into:
1. Numeric Types: Integers, Floating point numbers, and Complex numbers.
1. Integers (int): Represent whole numbers, positive or negative, without decimals,
of unlimited length. Commonly used in counting, indexing, and operations that
require precision without fractional parts.
• Integers (int): Whole numbers, positive or negative, without decimals of
unlimited length.
2. Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
2. Floating Point Numbers (float):
Represent real numbers and contain one or more decimals. Suitable for
measurements, scientific calculations, and any operation that requires fractional
numbers.
Precision might be an issue for very high precision requirements due to the way
floating-point numbers are stored.
• Floating Point Numbers (float): Numbers, positive or negative, containing one
or more decimals.
3. Complex Numbers (complex):
Written with a "j" as the imaginary part (x + yj), where x and y are floats.
Used in fields requiring complex number calculations such as signal processing,
engineering, and specific branches of mathematics.
• Complex Numbers (complex): Written with a "j" as the imaginary part: x + yj.
2. Sequence Types: Lists, Tuples, and Strings.
1. Lists (list):
Ordered and changeable (mutable) collections, allowing duplicate members.
Versatile for storing a sequence of objects that may need to be altered during the
program lifecycle, such as adding, removing, or changing elements.
2. Tuples (tuple):
Ordered collections like lists, but unchangeable (immutable).
Suitable for fixed data sets. They can be used as keys in dictionaries or as
elements of sets, where immutability is necessary.
3. Strings (str):
Ordered sequences of characters, making them immutable.
Used for text representation, including processing and manipulation of textual
data like names, messages, and outputs.
3. Mapping Type: Dictionary.
• Dictionary (dict): Unordered, changeable, and indexed collections, written
with curly brackets. Dictionaries have keys and values.
4. Set Types: Sets and Frozen Sets.
• Set (set): Unordered and unindexed collections, written with curly brackets.
However, unlike dictionaries, they are unordered collections of unique
elements.
• Frozen Set (frozenset): Immutable and hashable version of a set.
5. Boolean Type (bool): Represents True or False values and is used to perform logical
operations.
6. Binary Types: Binary, Bytearray, Memoryview.
• Bytes (bytes): Immutable sequence of bytes.
• Bytearray (bytearray): Mutable sequence of bytes.
• Memoryview (memoryview): Memory view object of the byte data.
Variables in Python
Variables in Python are created by a simple assignment operation, with the variable name on
the left, the assignment operator =, and the value on the right. Python infers the type of the
variable based on the value assigned to it.
• Dynamic Typing: Python allows you to reassign variables to different data types.
• Naming Conventions: Variable names can be short (a, x, y) or descriptive (age,
car_speed, total_volume). However, they must start with a letter or an underscore,
cannot begin with a number, and are case-sensitive.
3. Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
2.3 Mutable vs Immutable Data Types
• Mutable: Their content can be changed without changing their identity. Examples
include lists, dictionaries, and sets.
• Immutable: Their content cannot be changed once they are created. Examples include
integers, floats, strings, and tuples.
Summary
Understanding data types and variables is fundamental in Python as it affects how data can be
manipulated and stored. Each data type in Python is designed with a specific purpose in mind,
offering a wide range of functionalities to perform various operations on data efficiently.
Variables in Python are easy to declare and use, promoting a cleaner and more readable
codebase.