Uploaded by rafmajeed

Python Programming Notes: Basics, Data Types, Operators

advertisement
PYTHON NOTES
Programming is the process of designing and building executable computer programs to accomplish specific
computing tasks. It involves writing code using a set of instructions that a computer can understand and execute.
The code is written in a programming language, which acts as a bridge between human-readable commands and
the machine's binary operations. Programming enables the development of software, applications, websites, and
more by defining how data is processed and how operations are carried out.
Why Do We Need Python Programming Language for Data Analytics?
Python is widely used in data analytics due to its simplicity, versatility, and powerful libraries. Here are a few
reasons why Python is essential for data analytics:
1. Ease of Use: Python has a straightforward and readable syntax, making it accessible for beginners and
professionals alike.
2. Comprehensive Libraries: Python offers a range of libraries such as:
3. Pandas: For data manipulation and analysis.
4. NumPy: For numerical computations.
5. Matplotlib and Seaborn: For data visualization.
6. Community Support: Python has a large and active community, ensuring abundant resources, tutorials,
and support.
7. Integration Capabilities: Python can easily be integrated with other technologies and databases, which
is crucial for large-scale data analysis.
What is an IDE?
An IDE (Integrated Development Environment) is a software application that provides comprehensive facilities
for programmers for software development. It typically includes a code editor, debugger, and compiler or
interpreter. An IDE helps streamline the process of writing, testing, and debugging code.
1. PyCharm: Widely used for Python development.
2. Visual Studio Code: A flexible, open-source code editor.
3. Jupyter Notebook: Popular for data science and interactive coding.
What is a Print Statement & Why Do We Use It?
A print statement is a command used in many programming languages to display output on the screen. In Python,
the print() function is used to output text or variables.
Usage: The print() statement is used to:
Display messages or results to the user.
Debug code by showing the values of variables at different points in the program.
Example:
python
print("Hello, World!")
output = 5 * 3
print("The result is:", output)
Data Type:
Data types define the type of data that can be stored and manipulated within a program. Python supports several
data types:
FROM THE DESK OF: SIR NASIR HUSSAIN & SIR SHAHZAIB PARACHA
1
Numeric Types:
1. int: Integer numbers (e.g., 42)
2. float: Floating-point numbers (e.g., 3.14)
3. complex: Complex numbers (e.g., 3 + 4j)
Sequence Types:
1. str: Strings (e.g., "Hello, World!")
2. list: Lists (e.g., [1, 2, 3, 4])
3. tuple: Tuples (e.g., (1, 2, 3, 4))
Mapping Type:
1. dict: Dictionaries (e.g., {"key1": "value1", "key2": "value2"})
Boolean Type:
1. bool: Represents True or False.
What is a Variable?
A variable is a named location in memory that stores data. It acts as a container for data values that can change
during program execution.
Example:
python
age = 25
name = "John"
height = 5.9
Valid Variable
1. A valid variable name follows specific rules:
2. Must start with a letter (a-z, A-Z) or an underscore (_).
3. It can contain letters, numbers (0-9), and underscores.
4. Cannot start with a number.
5. Cannot use reserved words (e.g., print, if, else).
Example of Valid Variables:
python
name = "Alice"
_age = 30
user123 = "John"
Invalid Variable
An invalid variable name violates the rules mentioned above. Examples of Invalid Variables:
python
123name = "Bob" # Starts with a number
user-name = "Alice" # Contains a hyphen
if = 5 # Uses a reserved word
What are Operators?
Operators are special symbols used to perform operations on variables and values. Python supports several types
of operators:
FROM THE DESK OF: SIR NASIR HUSSAIN & SIR SHAHZAIB PARACHA
2
Arithmetic Operators
Used to perform basic mathematical operations.
1.
2.
3.
4.
5.
6.
7.
Addition (+): a + b
Subtraction (-): a - b
Multiplication (*): a * b
Division (/): a / b
Modulus (%): a % b (remainder of division)
Exponentiation (**): a ** b (power)
Floor Division (//): a // b (integer division)
Example:
python
x = 10
y=3
print(x + y) # Output: 13
print(x % y) # Output: 1
Comparison Operators
Used to compare values and return a Boolean result (True or False).
1.
2.
3.
4.
5.
6.
Equal to (==): a == b
Not equal to (! =): a != b
Greater than (>): a > b
Less than (<): a < b
Greater than or equal to (>=): a >= b
Less than or equal to (<=): a <= b
Example:
python
a=5
b = 10
print(a > b) # Output: False
print(a <= b) # Output: True
FROM THE DESK OF: SIR NASIR HUSSAIN & SIR SHAHZAIB PARACHA
3
Download