Skip to main content

Learn Python in 10 days

     10 DAYS OF LEARNING PYTHON

 for more follow @ https://infowithmottale.blogspot.com/

     


 

Day 1: Learn basic Python Syntax -

Download and install a Python interpreter, learn how to run a Python script, understand basic Python syntax and print statements.

Basic Python Syntax

 

# A comment, this is so you can read your program later.

# Anything after the # is ignored by python.

 

print("I could have code like this.") # and the comment after is ignored

 

# You can also use a comment to "disable" or comment out code:

# print("This won't run.")

 

print("This will run.")

 

How to run a Python script

1. Save your Python script to a local directory.

 

2. Open your terminal or command prompt and navigate to the directory where you saved your Python script.

 

3. Type “python yourPythonScript.py” and hit enter. This will execute your Python script.

 

4. If you have any arguments you would like to pass to your script, you can type them after the script name, separated by spaces. For example, “python yourPythonScript.py argument1 argument2”.

 

Day 2: Understand Data Types and Variables -

 Learn about Python data types, variables, and how to perform basic operations on them.

Python Data Types: 

Python supports several data types including strings, integers, floats, booleans, lists, tuples, sets, and dictionaries.

 

Strings are sequences of characters, such as letters, numbers, and symbols. They are declared by enclosing them within single or double quotation marks.

 

Integers are whole numbers, both positive and negative, without any decimal points.

 

Floats are numbers with decimal points.

 

Booleans are logical data types that can have only two possible values: True or False.

 

Lists are sequences of values that are mutable, meaning they can be changed. They are declared by enclosing the values within square brackets.

 

Tuples are sequences of values that are immutable, meaning they cannot be changed. They are declared by enclosing the values within parentheses.

 

Sets are unordered collections of unique values. They are declared by enclosing the values within curly brackets.

 

Dictionaries are data structures that store key-value pairs. They are declared by enclosing the key-value pairs within curly brackets.

 

Variables:

Variables are the names used to store data values in Python. Variables can be assigned different types of data and can be used to perform operations on the data.

 

Basic Operations:

In Python, basic operations can be performed on data types using operators.

 

Arithmetic operators such as +, -, *, /, //, %, and ** can be used to perform basic math operations.

 

Comparison operators such as ==, !=, >, <, >=, and <= can be used to compare values.

 

Logical operators such as and, or, and not can be used to evaluate multiple conditions.

 

Assignment operators such as =, +=, -=, *=, and /= can be used to assign a value to a variable.

 

DAY 3: Control Flow and Loops -

Learn about control flow, logic, and looping in Python.

Control flow in Python is the order in which the code is executed. Logic is the set of principles that help us evaluate and make decisions about our code. Looping is a process of repeating a set of instructions until a certain condition is met.

 

Control flow in Python is controlled by the use of if/else statements, for and while loops, and try/except statements. If/else statements allow us to execute code based on certain conditions, for and while loops allow us to repeat code until a certain condition is met, and try/except statements allow us to catch and handle errors.

 

Logic in Python is used to evaluate conditions, make decisions, and perform calculations. Logic statements such as if/else statements and boolean operators (and, or, not) are used to evaluate conditions, while comparison operators (>, <, ==, etc.) are used to compare values and make decisions.

 

Looping in Python is a way of repeating a set of instructions until a certain condition is met. The most common looping structure is the for loop, which is used to iterate through a sequence of items. The while loop is another looping structure which is used to repeat code while a certain condition is true.

 

DAY 4: Functions and Modules - 

Learn about writing functions and using built-in Python modules.

Writing functions in Python involves defining a function with parameters and a return value. To define a function, you use the keyword def followed by the function name and parentheses containing any parameters. Parameters are variables that can be used in the function and are specified when the function is called. The body of the function is indented and contains the code that will be executed when the function is called. The return statement is used to return a value from the function.

 

Python also provides many built-in modules that can be imported and used in your code. These modules provide additional functionality such as mathematical calculations, file input/output, and string manipulation. To use a built-in module, you must first import it. This is done with the import keyword followed by the name of the module. You can then access the functions and variables provided by the module.

 

DAY 5: Working with Files -

Learn how to read and write files in Python.

Reading and writing files in Python is easy and straightforward. To read a file, use the open() function with the file name as an argument. This will open the file and return a file object. To read the contents of the file, call the read() method on the file object. This will return the contents of the file as a string.

 

To write to a file, you can use the write() method on the file object. If you want to add data to an existing file, you can open the file in "append" mode. This will allow you to add data to the end of the file.

 

For example:

 

# Open a file in read mode

f = open("myfile.txt", "r")

 

# Read the contents of the file

data = f.read()

 

# Close the file

f.close()

 

# Open a file in write mode

f = open("myfile.txt", "w")

 

# Write some data to the file

f.write("This is some text")

 

# Close the file

f.close()

 

DAY 6: Working with Databases -

Learn how to connect to and query a database in Python.

1. Import the needed libraries:

 

In order to connect to and query a database in Python, you will need to import the necessary libraries. This will typically involve importing the ‘sqlite3’ library for working with SQL databases, and the ‘psycopg2’ library for working with Postgres databases.

 

2. Establish a connection:

 

Once the necessary libraries have been imported, will involve creating a new connection object and passing in the relevant connection parameters, such as the database name, username, password, and host.

 

3. Execute queries:

 

Once the connection has been established, you can then execute queries against the database. This is typically done using the cursor object created when establishing the connection. This will allow you to execute SQL statements, such as SELECT, INSERT, UPDATE, and DELETE statements.

 

4. Fetch the results:

 

Once the query has been executed, you can then fetch the results from the cursor. This can be done using the cursor’s ‘fetchall()’ method, which will return all of the rows from the query. Alternatively, you can use the ‘fetchone()’ method to return a single row.

 

5. Close the connection:

 

Once you have finished executing queries and fetching the results, it is important to close the connection. This can be done using the connection’s ‘close()’ method. This will ensure that all resources are freed up, and that the database connection is closed properly.

 

DAY 7: Object-Oriented Programming -

Learn the fundamentals of object-oriented programming in Python.

Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. In OOP, objects are seen as self-contained entities that have properties, methods, and behavior, such people, cars and animals.

 

The fundamentals of OOP in Python include:

 

- Class: A template that defines the characteristics of an object.

- Object: An instance of a class.

- Attribute: A variable that belongs to a particular class or object.

- Method: A function that belongs to a particular class or object.

- Inheritance: A process where one class inherits the attributes and methods from another class.

- Polymorphism: The ability to use the same code on different objects.

- Encapsulation: The process of binding together the data and functions that operate on that data.

 

In Python, OOP is used to create classes, which are templates for creating new objects. Each class contains attributes, which are variables that store information about the object, and methods, which are functions that perform specific tasks related to the object.

 

Classes allow for the reuse of code and the implementation of inheritance, which is when a class inherits the attributes and methods of another class. This allows for the creation of complex programs with less code.

 

In addition, Python supports polymorphism, which is the concept of having different objects that can respond differently to the same method. This allows for the creation of more flexible applications.

 

 

DAY 8: Working with Web Services - 

Learn how to use web services in Python.

Using web services in Python is quite simple, and can be done in a few steps.

 

1. Install the Requests library: Requests is a popular Python library that simplifies making HTTP requests. It can be installed using the command line: pip install requests

 

2. Create a web service request object: To create a web service request object, you will need to specify the URL for the web service, the method for the request, and any parameters that need to be passed to the web service.

 

3. Make the web service request: Once you have created the web service request object, you can make the request by calling the request.get() or request.post() functions.

 

4. Process the response: Once the web service request is complete, you will need to process the response. Depending on the type of web service you are using, this could involve parsing the response data, or converting it into an appropriate format.

 

5. Handle any errors: If there are any errors with making the web service request, you will need to handle them appropriately. This could involve raising an exception, logging the error, or displaying an error message to the user.

 

DAY 9: Working with APIs - 

Learn how to use APIs in Python.

Using APIs in Python is easy and straightforward. First, you must locate the API you want to use and get the API URL. Next, you will need to import the requests library to make API calls. After that, you can use the requests.get() method to make a request to the API. When the API responds, you can use the requests.json() method to parse the response into a Python dictionary. Finally, you can use the data to do whatever you want.

For example, if you wanted to find the current temperature in London, you could use the OpenWeatherMap API. The API URL would be https://api.openweathermap.org/data/2.5/weather?q=London. You would then use the requests.get() method to make a request and the requests.json() method to parse the response. Finally, you can access the data from the response, such as the current temperature.( https://api.openweathermap.org/data/2.5/weather?q=London. )

 

DAY 10: Debugging and Testing -

1. Use the built-in Python Debugger (PDB): PDB is a built-in Python module that allows you to set breakpoints in your code, step through lines, view the current state of variables and more. To use PDB, simply import it and call the set_trace() function at the point you want to start debugging.

 

2. Use print statements: If you’re not a fan of setting up breakpoints and stepping through code, you can also use print statements to debug your code. Simply insert print statements wherever you want to check the state of a variable or the output of a function.

 

3. Use a linter: A linter is a tool that reads your code and checks for errors and potential problems. It’s a great way to catch common mistakes and typos that can lead to bugs or unexpected behavior.

 

4. Use unit testing: Unit testing is a type of software testing in which individual units or components of a system are tested. By writing unit tests for your code, you can quickly identify and fix any bugs. The unittest module is a great way to do this in Python.

 

5. Use a debugger: A debugger is a tool that allows you to step through code, view the current state of variables, and more. It’s a great way to pinpoint exactly where a bug is occurring. PyCharm is a popular Python IDE that includes a built-in debugger.

Comments