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

Python

Python employs a hybrid execution model that includes both compilation to bytecode and interpretation via the Python Virtual Machine (PVM). Users can interact with Python through an interactive shell (REPL), run scripts from files, or utilize Integrated Development Environments (IDEs) for a more comprehensive coding experience. The document also outlines core syntactic elements, data types, programming structures, and type conversion methods in Python.

Uploaded by

Mandeep Singh
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)
7 views5 pages

Python

Python employs a hybrid execution model that includes both compilation to bytecode and interpretation via the Python Virtual Machine (PVM). Users can interact with Python through an interactive shell (REPL), run scripts from files, or utilize Integrated Development Environments (IDEs) for a more comprehensive coding experience. The document also outlines core syntactic elements, data types, programming structures, and type conversion methods in Python.

Uploaded by

Mandeep Singh
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

Python is commonly described as an interpreted language, but

technically it follows a hybrid execution model that involves both


compilation and interpretation.
1. The Two-Step Execution Process
When you run a Python script (e.g., python [Link] ), the following
steps occur automatically behind the scenes:
Step 1: Compilation (to Bytecode)
The source code is first translated by the Python compiler into an
intermediate, lower-level representation called bytecode. This bytecode
is platform-independent and is often cached in .pyc files (found in
a __pycache__ directory) to speed up subsequent runs.
Step 2: Interpretation (via PVM)
The Python Virtual Machine (PVM)—the heart of the Python interpreter
—reads and executes the bytecode line-by-line. It translates these
instructions into machine code that your computer's CPU can
understand.
Interacting with Python programs can be done in various ways,
from directly typing commands into an interactive shell to running
complex scripts via an Integrated Development Environment (IDE).
Real Python Tutorials +1
Running Python Interactively (REPL)
The quickest way to start is using the Read-Eval-Print Loop (REPL), also
known as the interactive shell.
Real Python Tutorials
How to access: Open your command line interface (Command Prompt,
PowerShell, or Terminal) and
type python (or python3 or py depending on your setup) and press
Enter.
How it works: The prompt changes to >>> , indicating Python is ready
for your input. You type single lines of Python code, press Enter, and the
interpreter immediately executes the code and prints any output.
Use case: Excellent for testing small snippets of code, experimenting
with syntax, and debugging.
Exiting: Type exit() or quit() and press Enter, or use the keyboard
shortcut Ctrl + Z and Enter on Windows, or Ctrl + D on Linux/macOS.
Real Python Tutorials +4
Running Python Scripts from a File
For larger, reusable applications, you write your code in a plain text file,
typically with a .py extension, and then execute the file using the
interpreter.
Real Python Tutorials +1
How to create: Use any text editor or IDE (like VS Code, PyCharm, or
Python's built-in IDLE) to write your code and save it as a .py file.
How to run:
Open your command line interface.
Navigate to the directory where you saved your file.
Run the script using the command: python [Link] (e.g., python
[Link] ).
Real Python Tutorials +2
Using an Integrated Development Environment (IDE)
IDEs like VS Code or PyCharm offer a comprehensive environment that
combines code editing, execution, and debugging features. They often
include syntax highlighting, code completion, and integrated debuggers,
streamlining the development process.
Real Python Tutorials +1
Interacting with Programs via User Input
Within a running Python program (either interactively or as a script), you
can get input from a user using the built-in input() function.
GeeksforGeeks +1
The program will pause and wait for the user to type something and
press Enter.
The input is always captured as a string, so you may need to convert it
to a number using int() or float() if necessary.
Example in a script:
python
name = input("Enter your name: ")
print("Hello,", name, "!")
This code would first prompt the user with "Enter your name: ", wait for
input, and then print a greeting using the entered name.
Python is composed of several fundamental building blocks that define
its syntax and logic. These elements are used together to construct
programs, much like bricks building a house.
YouTube
Core Syntactic Elements
Keywords: Reserved words like if , else , and for that have specific
predefined meanings in Python.
Identifiers: Unique names given to variables, functions, or classes; they
must follow specific rules, such as not starting with a number.
Indentation: Unlike many languages, Python uses whitespace
(indentation) to define code blocks instead of curly braces.
Comments: Text preceded by # that is ignored by the interpreter, used
for documenting code.
[Link] +1

Data and Operations


Variables: Containers used to store data values for later use in a script.
Literals: The actual constant values assigned to variables, such as
numbers ( 5 ) or strings ( "Hello" ).
Data Types: Classification of data, including:
Numeric: int (integers), float (decimals), and complex .
Text: str (strings).
Boolean: bool ( True or False ).
Operators: Symbols used to perform operations, such
as Arithmetic ( + , - ), Comparison ( == , > ), and Logical ( and , or ).
YouTube +6

Programming Structures
Control Flow: Statements that manage the order of execution,
including Selection ( if/else ) and Iteration (loops like for and while ).
Functions: Reusable blocks of code defined with the def keyword.
Data Structures: Built-in collections used to store multiple items, such
as Lists, Tuples, Dictionaries, and Sets.
Objects and Classes: Components of Python's object-oriented nature;
nearly everything in Python is an object.
Python documentation +5
Would you like a detailed breakdown or code examples for a specific
element, such as lists or control flow?
Type conversion in Python is the process of changing a value's data
type, which is essential for ensuring compatibility and performing correct
operations. Python supports two primary types: Implicit (automatic)
and Explicit (manual) conversion, also known as type casting.
Programiz +2
Implicit Type Conversion
Implicit conversion is performed automatically by the Python interpreter
to avoid data loss when different compatible data types are used in an
expression. The lower data type is converted to the higher data type.
Programiz +2
Example: Integer to Float
When an integer and a float are added, Python automatically converts
the integer to a float to ensure the result maintains decimal precision.
python
num_int = 10 # int
num_float = 5.5 # float

result = num_int + num_float

print("Result:", result)
print("Type of result:", type(result))
# Output:
# Result: 15.5
# Type of result: <class 'float'>
Python does not perform implicit conversion between incompatible
types, such as a string and an integer, which would result in
a TypeError .
Programiz +1
Explicit Type Conversion (Type Casting)
Explicit conversion is when the programmer manually converts a data
type using built-in functions. This gives the developer precise control
over the conversion process, though it can lead to data loss (e.g.,
converting a float to an integer truncates the decimal part).
Scaler +1
Commonly used functions include:
Mimo +1
int() : Converts a value to an integer.
float() : Converts a value to a floating-point number.
str() : Converts any data type into a string.
list() , tuple() , set() , dict() : Convert between collection types.
Example 1: String to Integer for Calculation
User input is always read as a string, so explicit conversion is necessary
for mathematical operations.
python
age_str = "25" # A string

# Explicitly convert the string to an integer


age_int = int(age_str)

years_until_100 = 100 - age_int


print("Years until 100:", years_until_100)
print("Type of new variable:", type(years_until_100))
# Output:
# Years until 100: 75
# Type of new variable: <class 'int'>
Example 2: Float to Integer (Data Loss)
Converting a float to an integer truncates the decimal value.
python
num_float = 9.7 # A float

# Explicitly convert the float to an integer


num_int = int(num_float)

print("Integer value:", num_int)


# Output:
# Integer value: 9
Example 3: Integer to String for Concatenation
Numbers must be converted to strings for concatenation with other
strings.
python
score = 95 # An integer

# Explicitly convert the integer to a string


result = "Your score is: " + str(score)

print(result)
# Output:
# Your score is: 95

You might also like