Basic Syntax and First program in Python

Overview

Python is a high-level programming language that is easy to learn and widely used for web development, data analysis, artificial intelligence, and many other applications because of it’s simplicity it is widely used. In this tutorial, we will discuss the basic syntax in Python and how to write your first program in Python.

Basic Syntax in Python:

  1. Comments: 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:

#**This is a comment**
print("Hello, Let's begin your Journey for becoming a Data Scientist!")        

  1. Variables: In Python, variables are used to store values or we can say we allocate memory to a value. A variable is a container that holds a value, such as a number, string, or list.

Example:

mathlete = 5  #variable
deepdiver = "Andrew Ng" #variable
print(mathlete)
print(deepdiver)        

  1. Data Types: Python is dynamically typed, which means you don't need to declare the data type of a variable before using it, unlike other languages where we need to provide the data type with the variable name. Python automatically assigns the data type based on the value assigned to the variable it automatically interprets the data type of a variable.

There are several built-in data types in Python, including:

  • Numbers: In numbers, we have integers, floating-point numbers, and complex numbers.
  • Strings: A string is a sequence of characters that is enclosed in single or double quotes, it contains text.
  • Lists: A list is a collection of items that are ordered and changeable.
  • Tuples: A tuple is similar to a list, but it is immutable, which means that 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.
  • Booleans: A boolean is a data type that can only have two values: True or False.

Example:

mathmogul = 5 # integer
numeralninja = 2.5 # float
brainbooster = "AlmaBetter" # string
boolean = True # boolean
print(type(mathmogul))
print(type(numeralninja))
print(type(brainbooster))
print(type(boolean))        

  1. Operators: Python supports several operators, including arithmetic, comparison, and logical operators.

Example:

print(5 + 6) # addition
print(4 - 3) # subtraction
print(6 * 5) # multiplication
print(6 / 5) # division
print(7 % 5) # modulus
print(5 ** 3) # exponentiation
print(4 == 2) # equal to
print(4 != 2) # not equal to
print(6 > 4) # greater than
print(5 < 2) # less than
print(6 >= 6) # greater than or equal to
print(3 <= 5) # less than or equal to
print(3 > 2 and 8 < 10) # logical and
print(4 > 6 or 9 < 10) # logical or
print(not(3 > 4)) # logical not        


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 on "New Notebook" or open an existing notebook from your Google Drive in which 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:

print("Hello, Future Data Scientist!")
coursework=input("Write text which you want to show to the user in output")        

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 the console. These are two 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 on 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 a few basic concepts like comments, variables, data types, input, and output 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.

Arpita Gupta

HR recruiter at GI services | Data scientist Trainee -Almabetter |Freelance Model Resonance Edventures limited (IIT foundation) ?? || Purvanchal university??????

1 年

This was very helpful ,thankyou

要查看或添加评论,请登录

社区洞察

其他会员也浏览了