0% found this document useful (0 votes)
3 views5 pages

Python Notes

This document provides a comprehensive overview of Python programming, including its introduction, first program, character set, variables, identifier rules, data types, and operators. Python is a versatile, high-level language known for its readability and ease of use, supporting various programming paradigms. Key features include dynamic typing, an extensive standard library, and a wide range of built-in data types and operators.

Uploaded by

aayugaeklabbe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Notes

This document provides a comprehensive overview of Python programming, including its introduction, first program, character set, variables, identifier rules, data types, and operators. Python is a versatile, high-level language known for its readability and ease of use, supporting various programming paradigms. Key features include dynamic typing, an extensive standard library, and a wide range of built-in data types and operators.

Uploaded by

aayugaeklabbe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Below is a comprehensive note on Python, covering the requested topics: Introduction to Python,

First Program, Character Set, Variables and Their Usage, Rules for Identifiers, Data Types, and
Operators.

Python Programming Note


Introduction to Python
Python is a high-level, interpreted, general-purpose programming language created by Guido van
Rossum and first released in 1991. It is known for its simplicity, readability, and versatility,
making it ideal for beginners and professionals alike. Python supports multiple programming
paradigms, including procedural, object-oriented, and functional programming. It is widely used
in web development, data science, automation, artificial intelligence, and more due to its
extensive libraries and frameworks.

Key Features of Python:

 Simple and Readable Syntax: Python’s syntax is clear and resembles English, reducing
the learning curve.
 Interpreted Language: Code is executed line-by-line, eliminating the need for
compilation.
 Cross-Platform: Runs on Windows, macOS, Linux, and more.
 Extensive Standard Library: Provides modules and packages for various tasks.
 Dynamically Typed: No need to declare variable types explicitly.
 Open-Source: Free to use and distribute with a large community for support.

First Program in Python


The simplest Python program is the classic "Hello, World!" example. It demonstrates how to
output text to the console.

python
Copy
# First Python program
print("Hello, World!")

Explanation:

 print() is a built-in function that outputs text to the console.


 "Hello, World!" is a string (text) enclosed in quotes.
 The program can be saved with a .py extension (e.g., hello.py) and run using a Python
interpreter.
How to Run:

1. Install Python from python.org.


2. Save the code in a file (e.g., hello.py).
3. Open a terminal, navigate to the file’s directory, and run: python hello.py.
4. Output: Hello, World!

Character Set of Python


Python uses the Unicode character set (UTF-8 by default), allowing it to handle a wide range of
characters, including letters, digits, symbols, and emojis. The character set includes:

 Letters: Uppercase (A-Z) and lowercase (a-z).


 Digits: 0-9.
 Special Characters: @, #, $, %, ^, &, *, (, ), _, +, -, /, \, |, {, }, [, ], :, ;, <, >, ,, ., ?, !, ", ',
~, `, etc.
 Whitespace: Spaces, tabs (\t), newlines (\n).
 Unicode Characters: Supports multilingual characters (e.g., 日本語, 😊).

Python source code is written in plain text, and the interpreter processes these characters to
execute the program.

Variables and How to Use Them


Variables are used to store data in memory for processing. In Python, variables are created when
a value is assigned to a name using the assignment operator (=).

Syntax:

python
Copy
variable_name = value

Example:

python
Copy
# Declaring variables
name = "Alice" # String
age = 25 # Integer
height = 5.5 # Float

# Using variables
print(name, "is", age, "years old and", height, "feet tall.")

Key Points:

 Dynamic Typing: No need to specify the data type; Python infers it (e.g., age = 25
makes age an integer).
 Reassignment: Variables can be reassigned to different types (e.g., age = "twenty-five"
changes age to a string).
 Memory Allocation: Variables reference memory locations, and Python’s garbage
collector manages memory.
 Variable Scope: Variables can be local (inside a function) or global (defined outside).

Rules for Identifiers


Identifiers are names given to variables, functions, classes, etc. Python has strict rules for valid
identifiers:

1. Allowed Characters:
o Letters (A-Z, a-z), digits (0-9), and underscore (_).
o Example: my_variable, count_1, MyClass.
2. First Character:
o Must start with a letter or underscore, not a digit.
o Valid: name, _temp, Class1.
o Invalid: 1name, 2count.
3. Case Sensitivity:
o Python is case-sensitive (e.g., Name and name are different).
4. No Reserved Words:
o Cannot use Python keywords (e.g., if, for, while, class, return) as identifiers.
o Full list of keywords: False, None, True, and, as, assert, async, await, break, class,
continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is,
lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield.
5. No Special Characters:
o Symbols like @, $, %, etc., are not allowed (except _).
o Invalid: my@var, price$.
6. Length:
o No strict limit, but keep identifiers meaningful and concise (e.g., student_name is
better than s).

Conventions (PEP 8):

 Use snake_case for variables/functions (e.g., my_variable).


 Use CamelCase for classes (e.g., MyClass).
 Use uppercase for constants (e.g., MAX_VALUE).

Data Types in Python


Python has built-in data types to represent different kinds of data. The main types are:

1. Numeric Types:
o int: Integer numbers (e.g., 42, -10).
o float: Floating-point numbers (e.g., 3.14, -0.001).
o complex: Complex numbers (e.g., 3 + 4j).
2. Sequence Types:
o str: Strings, immutable sequences of characters (e.g., "Hello", 'World').
o list: Ordered, mutable collections (e.g., [1, 2, 3], ["apple", "banana"]).
o tuple: Ordered, immutable collections (e.g., (1, 2, 3), ("a", "b")).
3. Mapping Type:
o dict: Key-value pairs (e.g., {"name": "Alice", "age": 25}).
4. Set Types:
o set: Unordered, mutable collections of unique elements (e.g., {1, 2, 3}).
o frozenset: Immutable version of a set (e.g., frozenset([1, 2, 3])).
5. Boolean Type:
o bool: Represents True or False.
6. None Type:
o NoneType: Represents the absence of a value (e.g., None).

Type Checking: Use the type() function to check a variable’s type:

python
Copy
x = 42
print(type(x)) # Output: <class 'int'>

Type Conversion: Convert between types using functions like int(), float(), str(), etc.:

python
Copy
num_str = "123"
num_int = int(num_str) # Converts to 123 (integer)

Operators in Python
Operators are symbols that perform operations on variables and values. Python supports the
following categories:

1. Arithmetic Operators:
o +: Addition (e.g., 5 + 3 = 8).
o -: Subtraction (e.g., 5 - 3 = 2).
o *: Multiplication (e.g., 5 * 3 = 15).
o /: Division (e.g., 5 / 2 = 2.5).
o //: Floor division (e.g., 5 // 2 = 2).
o %: Modulus (remainder) (e.g., 5 % 2 = 1).
o **: Exponentiation (e.g., 2 ** 3 = 8).
2. Comparison Operators:
o ==: Equal to (e.g., 5 == 5 is True).
o !=: Not equal to (e.g., 5 != 3 is True).
o >: Greater than (e.g., 5 > 3 is True).
o <: Less than (e.g., 3 < 5 is True).
o >=: Greater than or equal to.
o <=: Less than or equal to.
3. Logical Operators:
o and: True if both operands are true (e.g., True and False is False).
o or: True if at least one operand is true (e.g., True or False is True).
o not: Negates the truth value (e.g., not True is False).
4. Assignment Operators:
o =: Assigns a value (e.g., x = 5).
o +=: Adds and assigns (e.g., x += 3 is x = x + 3).
o -=, *=, /=, //=, %=, **=: Similar compound assignments.
5. Bitwise Operators:
o &: Bitwise AND (e.g., 5 & 3 is 1).
o |: Bitwise OR (e.g., 5 | 3 is 7).
o ^: Bitwise XOR (e.g., 5 ^ 3 is 6).
o ~: Bitwise NOT (e.g., ~5 is -6).
o <<: Left shift (e.g., 5 << 1 is 10).
o >>: Right shift (e.g., 5 >> 1 is 2).
6. Membership Operators:
o in: Checks if a value is in a sequence (e.g., 3 in [1, 2, 3] is True).
o not in: Checks if a value is not in a sequence.
7. Identity Operators:
o is: Checks if two variables refer to the same object (e.g., x is y).
o is not: Checks if they are different objects.

Example:

python
Copy
x = 10
y = 3
print(x + y) # Output: 13
print(x > y) # Output: True
print(x in [5, 10, 15]) # Output: True

You might also like