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

? Getting Started With Python

The document provides a comprehensive introduction to Python, covering its definition, features, execution modes, keywords, variables, data types, operators, expressions, input/output, type conversion, and debugging. It highlights Python's ease of use, portability, and rich library support, making it suitable for various applications such as web development and data science. Additionally, it explains the differences between interactive and script modes, as well as common errors encountered in Python programming.

Uploaded by

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

? Getting Started With Python

The document provides a comprehensive introduction to Python, covering its definition, features, execution modes, keywords, variables, data types, operators, expressions, input/output, type conversion, and debugging. It highlights Python's ease of use, portability, and rich library support, making it suitable for various applications such as web development and data science. Additionally, it explains the differences between interactive and script modes, as well as common errors encountered in Python programming.

Uploaded by

aamirabbasi1438
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🐍 Getting Started with Python – Full

Explanation

1. What is Python?
 A program is a set of instructions for the computer.
 Computers understand machine language (0s and 1s), but it’s too hard for humans.
 High-level languages (like Python, C++, Java) are easier for humans to write.
 Python uses an interpreter → executes line by line.

👉 Advantage: easy debugging, but slower than compiled languages.


👉 Compiler: translates whole program at once (faster but errors shown later).

2. Features of Python
 High-level → easy to read/write.
 Free & open source → no cost, community support.
 Interpreted → executes line by line.
 Simple syntax → looks like English, uses indentation.
 Case-sensitive → Number ≠ number.
 Portable → runs on Windows, Mac, Linux.
 Rich library → supports math, data, web, AI, etc.
 Used in web apps, AI, ML, data science, automation.

3. Python Execution Modes


a) Interactive Mode

 Works like a calculator.


 Type commands at >>> prompt → result shown immediately.
 Good for testing small code.
Example:
>>> 2+3
5

b) Script Mode

 Write code in .py file.


 Save and run the program.
 Good for long programs.

4. Keywords & Identifiers


 Keywords = reserved words with special meaning (if, else, while, True, False,
return, break).
 Cannot be used as variable names.
 Identifiers = names given to variables/functions.
o Must start with a letter or _.
o Can contain letters, digits, _.
o Cannot start with a digit.
o No special symbols like @, #, $.
o Should be meaningful (e.g., marks1, not m1).

✅ Example:

marks1 = 50
marks2 = 60
avg = (marks1+marks2)/2

5. Variables & Comments


 Variable = name that stores data in memory.
 No need to declare type (Python decides automatically).
 Example:

x = 10 # integer
y = "Hello" # string

 Comments = notes in code, ignored by interpreter.


o Start with #.
o Used for documentation.

# This program adds two numbers


a = 5
b = 10
print(a+b) # prints 15
6. Data Types in Python
Python treats everything as an object.

a) Numbers

 int → whole numbers (e.g., 10, -3).


 float → decimal numbers (e.g., 3.14, -2.5).
 complex → numbers with j (e.g., 3+4j).
 bool → True, False.

b) Sequence Types

 String: collection of characters in quotes. "Hello", 'Python'.


 List: ordered, mutable. [1,2,3,"hi"].
 Tuple: ordered, immutable. (1,2,3,"hi").

c) Set

 Unordered, no duplicates. {1,2,3}.

d) Dictionary

 Key–value pairs.

student = {"name":"Ali", "age":18}

e) None

 Special type for "no value".

f) Mutable vs Immutable

 Mutable: can be changed (list, set, dict).


 Immutable: cannot be changed (int, float, string, tuple).

7. Operators in Python
Operators work on operands (values/variables).
 Arithmetic: + - * / % // **.
 Relational (comparison): == != > < >= <=.
 Assignment: = += -= *= /=.
 Logical: and or not.
 Identity: is, is not (check same memory object).
 Membership: in, not in.

Precedence (priority) of operators

 () → ** → * / % // → + - → comparison → logical → assignment.

8. Expressions & Statements


 Expression = combination of values, variables, operators (evaluates to value).
Example: 3+4*2 → 11.
 Statement = full instruction.
Example:

x = 5 # assignment statement
print(x) # print statement

9. Input & Output


 Input: input("message") → always takes input as string.

name = input("Enter name: ")


age = int(input("Enter age: ")) # type cast to int

 Output: print()
o sep → separator (default = space).
o end → ending (default = new line).

print("Hello", "World", sep="-", end="!")


# Output: Hello-World!

10. Type Conversion


Explicit (Type Casting)

 Done manually with functions: int(), float(), str(), chr(), ord().


x = "10"
y = int(x) # convert string to integer

Implicit (Type Coercion)

 Done automatically by Python (no data loss).

x = 10 # int
y = 2.5 # float
z = x + y # int + float → float
print(z) # 12.5

11. Debugging (Finding Errors)


Three types of errors in Python:

 Syntax Error: violates Python rules.


Example: print "Hello" ❌ (missing parentheses).
 Logical Error: program runs but gives wrong output.
Example: avg = a+b/2 instead of (a+b)/2.
 Runtime Error: occurs while program runs.
Example: divide by zero.

You might also like