Python Basics- Syntax and Data types

By | April 24, 2018

Before proceeding further Let me remind you that “Errors and Mistakes are Ok when you are in the process of learning. It’s fine for amateurs to experiment So don’t be afraid to try all possible ways.”

Let’s get familiar with Python Syntax structure:

1.Indentation in Python. Indentation means spacing. There will not be any curly braces{} in python to distinguish blocks of code. Instead of { } python will use indenting to determine the structure of code.

2. Anyline followed by # is a comment. Unlike other programming languages Python doesn’t support multi line comment

3. Give farewell to semicolon(;) if you are programming in python. There should be no lines ending with semicolon(;)

  This is how our very own helloworld program will look in python3.

   #Program to print helloworld to console.

   print(‘hello world’)

Run as Python Run.

   Output: hello world.                     

You can see first line is a comment. Will go in detail about print() function in upcoming posts.Before proceeding further lets brush up the basic concepts of a programming language:

1.Naming a variable:

How one can name/define a variable in Python:

  1. It can be only one word.
  2. It can use only letters, numbers, and the underscore (_) character.
  3. It can’t begin with a number.
  4. Names are subjected to case sensitivity(Variable names are case-sensitive, meaning that learnmode, Learnmode and learnMode are four different variables.)
  5. Keywords cannot be used as a name for a variable.

It is consider as a standard to start a variable name with lowercase however it is not mandate.

Basic Data types:

  1. Boolean:

Values can be either True or False. Integer 0 and the empty string “” are equivalent to False.

  1. Numeric Data types:
    • int– The integer (or int) data type indicates values that are whole numbers.
    • float – Will be used for decimal values such as 3.14, 1.729 etc
    • complex – Used for complex/imaginary numbers which are in a+ib form(Mostly used scientific research related fields.)
  2. Sequences:

      str: A sequence of Unicode characters. This is string data type

There are other data types under like list,tuple, byte,byte array,set,frozenset and dict. 

Among above int,float,boolean and string are of Primitive data types where as List,Tuple,dictionary and set are of Non Primitive data type.

We will revisit these topics in depth after discussing some basic programs.

Well we have committed that one need not to declare the data type during variable declaration.  What if I want to see the data type of a particular variable. Yes, you can know the data type by using type() function.

Code snippet:

#datatype
i=20
j=70.8
var=”Hello”
print(type(i))
print(type(j))
print(type(var))

output:

<class ‘int’>
<class ‘float’>
<class ‘str’>

Checkout that output gives type of each variable.

2.Operator Precedence:

I am assuming arithmetic operators are familiar for everyone so I am using the same here. Remaining logical, boolean and ternary operator will be discussed in later parts.

Let us revisit concept of operator precedence(How your interpreter will evaluate the given expression). It is similar to that of mathematics.

The ** operator is evaluated first; the *, /, //, and % operators are evaluated next, from left to right; and the + and – operators are evaluated last (also from left to right). You can use parentheses to override the usual precedence if you need to.

For those who are not aware of ** operator. It is an exponent operator.

So 2**3 =8 (It is nothing but 23=8). Click to check examples.

In addition to this learning, remember there is value None,  It is equivalent to null or nil/undefined in Other programming languages.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.