Basics of Coding
Python
Programming: A
Beginner's Guide
Welcome to the world of Python programming! This guide will serve
as your stepping stone into the exciting realm of code. We'll cover
fundamental concepts, starting with the basics of writing your first
lines of code and gradually delving into essential techniques like
using comments, executing commands, importing packages,
manipulating data, and much more.
by Lokesh Patel
Using Comments in Python
1 What are Comments? 2 Types of Comments
Comments are lines of text There are two main types
in your code that are of comments in Python:
ignored by the Python single-line comments
interpreter. They're used to (starting with '#' ) and
explain your code, make it multi-line comments (using
more readable, and help triple quotes """...""").
you understand how it
works.
3 Why Use Comments?
Comments are essential for making your code more
understandable, especially for yourself when you come back to
it later, and for other developers who might be working on your
code.
Executing Commands in
Python
Writing Code
You write your Python code in a text editor or an Integrated
Development Environment (IDE), like VS Code or PyCharm.
Saving Code
Save your code as a file with a .py extension, like "my_code.py".
Running Code
Open a terminal or command prompt, navigate to the
directory where your file is located, and use the "python"
command followed by the filename to run your code.
Importing Packages in Python
What are Packages? Import Statement
Packages in Python are You use the "import" statement
collections of modules that to bring in a package or a
provide pre-written functions specific module from a package.
and classes, which extend the The syntax is "import
functionality of the Python package_name" or "from
language. package_name import
module_name".
Using Imported Functions
Once a package or module is imported, you can access its functions
and classes using the dot notation (e.g.,
"package_name.function_name()").
Getting Data into Python
Input Functions Files Databases
Python provides built-in functions You can read data from files using For interacting with databases,
like "input()" that allow users to functions like "open()" and methods Python offers libraries like "sqlite3"
enter data directly while the like "read()" and "readlines()". and "psycopg2" that let you connect
program is running. Different file formats require specific to and query databases.
libraries.
Saving Output in Python
Function Description
print() Displays output to the console
or terminal.
open() Opens a file for writing or
appending data.
write() Writes data to an open file.
close() Closes an open file.
Accessing Records and Variables in Python
Variables 1
Variables are containers for storing data. You assign data to
variables using the assignment operator (=). For example,
"my_variable = 10" stores the value 10 in the variable 2 Data Structures
"my_variable". Python provides various data structures such as lists,
dictionaries, and tuples, which allow you to store and organize
Accessing Data 3 data in a structured manner.
You access data in variables and data structures using specific
methods. For example, you can access elements in a list using
their index (e.g., "my_list[0]"), and you can access elements in a
dictionary using keys (e.g., "my_dictionary['key']").
Conclusion and Next Steps
Learn More
Continue learning about Python by exploring more advanced concepts like loops,
conditional statements, functions, and object-oriented programming.
Practice
The key to mastering Python is through practice. Start with simple exercises and
gradually work on more complex projects.
Get Involved
Join online communities and forums to connect with other Python developers, share
knowledge, and get help with your projects.