bytes

tutorials

python

python syntax

Basic Syntax and First program in Python

Module - 1 Getting Started with Python
Basic Syntax and First program in Python

Read Time: 5 min 29 sec

Overview

Python 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:

Frame 31-min.png

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

mathlete = 5  #variable
deepdiver = "Andrew Ng" #variable
print(mathlete)
print(deepdiver)
  1. 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:

  1. Numbers 🔢 : We have integers, floating-point numbers, and complex numbers.
  2. Strings 📜 : A string is a sequence of characters in single or double quotes. It contains the text.
  3. Lists 📚 : A list is a collection of items that are ordered and changeable.
  4. Tuples 🔒 : A tuple is very similar to a list but immutable, meaning its values cannot be changed.
  5. Sets 🧮 : A set is a collection of unique elements.
  6. Dictionaries 📖 : A dictionary is a collection of key-value pairs that are unordered and changeable.
  7. Boolean : A boolean is a data type with only 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 "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:

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 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 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 :123:.
  • 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

  1. What category does Python fall under? 
    1. A low-level programming language 
    2. A high-level programming language 
    3. A markup language 
    4. An operating system

Answer: b: A high-level programming language

  1. What do Python comments mean? 
    1. Code that is executed by the Python interpreter 
    2. Notes for yourself or other developers that are ignored during program execution 
    3. A collection of items that are ordered and changeable 
    4. 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

  1. What is the used of variables in Python? 
    1. Storing values 
    2. Executing code 
    3. Commenting on code 
    4. Displaying output

Answer: a: Storing values

  1. What are the data types present in Python? 
    1. Numbers, strings, lists, tuples, sets, dictionaries, booleans 
    2. Integers, floating-point numbers, complex numbers 
    3. Strings 
    4. True or False

Answer: a: Numbers, strings, lists, tuples, sets, dictionaries, booleans

  1. What is the use of operators in Python? 
    1. Built-in functions that allow you to output text to the console 
    2. Code that is executed by the Python interpreter 
    3. Used to perform operations on variables and values 
    4. Notes for yourself or other developers that are ignored during program execution

Answer: c: Used to perform operations on variables and values

Related Programs
Full Stack Data Science with Placement Guarantee of 5+ LPA
Course
20,000 people are doing this course
Become a job-ready Data Science professional in 30 weeks. Join the largest tech community in India. Pay only after you get a job above 5 LPA.
Related Tutorials

AlmaBetter’s curriculum is the best curriculum available online. AlmaBetter’s program is engaging, comprehensive, and student-centered. If you are honestly interested in Data Science, you cannot ask for a better platform than AlmaBetter.

avatar
Kamya Malhotra
Statistical Analyst
Fast forward your career in tech with AlmaBetter
Vikash SrivastavaCo-founder & CPTO AlmaBetter
Vikas CTO
Related Tutorials to watch
Top Articles toRead
AlmaBetter
Made with heartin Bengaluru, India
  • Location
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2022 AlmaBetter