Python Syntax and First program in Python
Last Updated: 20th October, 2024Python is a high-level programming language that's simple to learn and broadly utilized for web advancement, data analysis, manufactured insights, and numerous other applications. Since of its straightforwardness, it is broadly utilized. This instructional exercise will talk about the essential sentence structure in Python and how to type in your to begin with program in Python
Basic Syntax in Python:
- đ Comments in Python are used to explain the Code or make notes for yourself or other developers who may work on the Code in the future. Comments are not executed by the Python interpreter and are ignored during the execution of the program. They start with a #ď¸âŁ symbol.
Example:
- Variables: Variables are used in Python to store values đž. They allocate memory to a value, making it accessible throughout the program. A variable acts as a container đŚ with a value, which could be a number đ˘, string đ¤, or list đ.
Example:
- Data types: Python is powerfully written, which implies you do not have to declare the data type of a variable sometime recently utilizing it, not at all like other languages where we ought to give the data type with the variable name. Python naturally allocates the data type based on the value allotted to the variable. It consequently interprets the data type of a variable.
There are several built-in data types in Python, including:
- Numbers đ˘ : We have integers, floating-point numbers, and complex numbers.
- Strings đ : A string is a sequence of characters in single or double quotes. It contains the text.
- Lists đ : A list is a collection of items that are ordered and changeable.
- Tuples đ : A tuple is very similar to a list but immutable, meaning its values cannot be changed.
- Sets 𧎠: A set is a collection of unique elements.
- Dictionaries đ : A dictionary is a collection of key-value pairs that are unordered and changeable.
- Boolean â
: A boolean is a data type with only two values: True or False.
Example:
- Operators:Â Python supports several operators, including arithmetic, comparison, and logical operators.
Example:
Steps for Python Programming
đ Step 1ď¸âŁ: Setting up Google Colab To get started, go to the Google Colab website at đ https://colab.research.google.com/. You will need to sign in with your Google account. Once you are signed in, you can create a new notebook by clicking "New Notebook" or open an existing notebook from your Google Drive where you want to write Code.
đStep 2ď¸âŁ: Writing your first Python program As soon as you click on the new notebook, your colab will appear, where you can see one code cell is already present. In the first cell of the notebook, type the following Code:
This is a very simple program that just prints the phrase "Hello, Future Data Scientist!" to the console. Python has a built-in function called "print()" that allows you to output text to the console whereas âinput()â allows to take input from console. These are two Python functions used for standard input and output in Python.
đ Step 3ď¸âŁ: Running your program
To run your program, simply click on the "play" button located on the left side of the cell or press "Shift+Enter" on your keyboard. You should see the output "Hello, World!" printed to the console below the cell.
đ Step 4ď¸âŁ:Saving your notebook
To save your notebook, click on "File" in the menu bar and then select "Save" or use the shortcut "Ctrl+S" on Windows or "Cmd+S" on Mac. You can also save your notebook to your Google Drive by clicking on "File" and then "Save a copy in Drive".
đ Step 5ď¸âŁ: Sharing your notebook
If you want to share your notebook with others, you can click on "Share" located in the upper-right corner of the notebook. This will allow you to share your notebook via a link or add collaborators who can view or edit your notebook.
Conclusion
Writing your first Python program in Google Colab is a simple and easy process. With Google Colab, you can write, run and share your Python code without having to install any software on your computer. In Python we have few basic concepts like comments, variables,data types, input and output in python which are mostly used go write your first program to get started.
KEY TAKEAWAY
- The key takeaway from this overview is that Python is a high-level programming language used for web development đ, data analysis đ, and artificial intelligence đ¤, among other applications.
- Python has a simple and easy-to-learn syntax đ, and it supports several data types and operators.
- Google Colab is a convenient tool to write âď¸, run, and share đ¤ Python code without having to install any software on your computer.
- The basic steps for writing a Python program in Google Colab include setting up an account , creating a new notebook đ, writing code đť, running the program , saving the notebook đž, and sharing it with others đĽ.
QUIZ
- What category does Python fall under?Â
- A low-level programming languageÂ
- A high-level programming languageÂ
- A markup languageÂ
- An operating system
Answer: b: A high-level programming language
- What do Python comments mean?Â
- Code that is executed by the Python interpreterÂ
- Notes for yourself or other developers that are ignored during program executionÂ
- A collection of items that are ordered and changeableÂ
- A data type that can only have two values: True or False
Answer: b: Notes for yourself or other developers that are ignored during program execution
- What is the used of variables in Python?Â
- Storing valuesÂ
- Executing codeÂ
- Commenting on codeÂ
- Displaying output
Answer: a: Storing values
- What are the data types present in Python?Â
- Numbers, strings, lists, tuples, sets, dictionaries, booleansÂ
- Integers, floating-point numbers, complex numbersÂ
- StringsÂ
- True or False
Answer: a: Numbers, strings, lists, tuples, sets, dictionaries, booleans
- What is the use of operators in Python?Â
- Built-in functions that allow you to output text to the consoleÂ
- Code that is executed by the Python interpreterÂ
- Used to perform operations on variables and valuesÂ
- Notes for yourself or other developers that are ignored during program execution
Answer: c: Used to perform operations on variables and values