python-fundamentals

6 Assertive Python Fundamentals for Beginners

Here in this blog we are going to cover python fundamentals. Are you willing to learn python for a better future in the programming field?

If yes, you are on the right track. Python is the best way to start your journey of programming.

Python is the most accepted and demanding language in the field of data science. So, if you are thinking of becoming a data scientist, python is a great skill for you.

The benefit of starting your programming learning with python is that it does not require any prior knowledge of coding.

But to learn python efficiently, you need to strengthen your basics. Without investing your time on basics you can’t do good with python.

 In this blog, we will explore Assertive python fundamentals for beginners that will help you to excel in this most valuable field.

Let’s start with the meaning of python.

Why is python known as python?

Have you ever thought about why python is called python?

Or

 Why the logo of a python representing a large snake?

Actually, the python name comes from a comedy sketch series(Monty Python’s Flying Circus) on BBC television. This show was very creative and well, so the developer was inspired by this name and developed the language by the python name.

What is python?

Python is a high level programming language developed in 1991 by Guido Van Rossum.

It is an object-oriented language and general-purpose language widely used in web development, scientific computing, etc.

Nowadays, Python is more popular than Java because of its simplicity and wide applications. 

Python Fundamentals

Fundamentals of Python involve basic building blocks of the Python programming language. As fundamentals of Python we are going to discuss about 13 assertive python fundamentals.

  • Statements
  • Indentations
  • Comments
  • Variables
  • Constants
  • Tokens
  •  Operators

Statements

Statements are logical instructions to read and execute by the interpreter. The statement can be single or multiple.

In Python, statements are of two types.

  • Expression Statements
  • Assignments Statements

Expression Statement: Expression statements are used to perform operations such as addition, division, subtraction, etc.

These statements are considered as expressions when they appear on the right side of an assignment and appear as a parameter to a method call.

See also  Most Useful Guide on Feature Selection Python

Remember: An expression should give a return value.

Let’s understand it by taking examples.

Case 1- When we use a simple arithmetic expression.

(2+6)*4 
32

Case 2- When we need to use a function in an expression.

div (4,2)
2

Assignment Statements

When we have to assign values, change values and create new values, assignment statements help us.

Syntax of Assignment Statement

# LHS <=> RHS
variable = expression

Assignment statements are further divided into 3 categories on the basis of value-based expression, current variable, and operation on the statement’s right-hand side.

  • Value-Based Expression on RHS

Here, when we need to assign a new variable, Python allocates a new memory location to this variable. 

Steps to check memory location-

  1. Firstly, we will create a string and assign this string to a variable “var”
  2. Then we will check the given memory location id for the variable.

Let’s understand it with a program-

var=”Guido Van Rossum”id(var)print(id(var))

Examples

var1=”Guido”id(var1)print(id(var1))
output-139623763211824
var2=”Guido”id(var2)print(id(var2)
output-139623763211824

Have you noticed Python allocates the same memory location for the same string into two different variables?

Do you know the reason?

 Ok, I am telling you why?

When the characters in the string are less than 20 without white spaces, and if the integer ranges from -5 to +255, the Python allocates the same memory by Python and is known as interning.

  • Current Python variable on RHS

Python does not allow a new memory location in this case.

Example-

current_var=”it’s about learning python”

print(id(current_var))

new_var=current_var

print(id(new_var))

Output-

140264590697072

140412047716976

Now, you can notice that for the two different variables, we have the same location id.

  • Operation on RHS

In this case we have an operation (a defining factor of the type of our statement) on the right side of the statement.

Example

test=4*3

type(test)

int

test1=4*3/5

type(test1)

Output-

float

Multiline Statement

We can define multiline statements in two ways.

  • Implicit Method
  • Explicit Method

The ending point of any statement in Python is taken as a newline character, and for the extension of a statement in multiple lines, two methods we can use.

Implicit Method: You can use parenthesis, ( ),{ }, and [ ].

Example-

x= (0+2+4+6+

8+10)

Explicit Method: You can use the continuation character, \’.

x= 1+3+5+ \

7+9

2. Indentation

In python programming, we use indentation to mark a set of code. The indent size should be four as per python style guidelines or PEP8(Python Enhancement Proposal).

Indentation is necessary for Python, unlike other programming languages.

                             Different blocks of code

3. Comments

Comments are the tagged lines of codes to increase the readability of the code. The main purpose of using taglines is to make the code self-explanatory.

See also  What Are the Top Uses of Python For Finance?

Comments are of two types-

Single line comment

Multiple line comment

Single line comment

We start a single-line comment with the help of single # simbol.

for example-

var=2*4

type(var)

# single-line comment

Multiline Comments

We can write multiple comments in Python by using “…”.

For example-

var1=4*2/5

type(var1)

comment line 1

comment line 2

comment line 3

Docstring Comments

It is a characteristic of Python to have a docstring or documentation string. It assists the programmer in adding quick notes in every module, function, class and method of Python.

We can define the string by using triple quotation marks (multiple comments)

If a string is placed just after a function, class definition, and on the top of a module, they will be considered as the docstring.

4. Variables

It is a memory address that we can change. A Variable is like a container where the data is stored. When we store the variable, it occupies the space in memory. 

A variable can be a number, letter, and underscore character. Such as i=1 or a=2.

If we can not change the variable, it is called constant. Python identifies the type of assigned variable because it is a type inferred language.

5. Constants

In Python, we rarely use the constants. It is a variable type that keeps values in it and can not be changed. 

Such as the value of a trigonometric function sin 90=1

and value of a light year= 9.46 *10^12 Km.

Also Read

6. Tokens

Tokens are considered as the smallest unit of the program and are of 4 types. 

  1. Reserved Words(Keywords)- Reserved words are fixed and special words in Python that convey a particular meaning. We can not use keywords as a variable in Python because Python is case-sensitive.
  1. Identifiers- These are the names defined by the users to express entities we can use in programs such as functions, classes, modules, variables and other objects.

We need to follow some rules when we are going to define identifiers.

  • When you are defining an identifier, you can use a series/sequence of letters in lowercase or uppercase. In addition, you can add digit numbers from 0 to 1 or an underscore.
  • At the start of the name of an identifier, don’t use digit numbers.
  • You are prohibited from using reserved words to express an identifier.
  • Name the identifier within 79 characters as PEP8 guidelines.
See also  Python vs Matlab: Which One Is the Best Language

3.Literals- Literals can be represented as data provided in a variable or constant. Python offers 5 literals.

String Literals- It is a sequence of characters that is surrounded by single, double, and triple quotes for a string.

Numeric Literals– Numeric literals are unalterable such as integers, float, and complex.

Boolean Literals– True and false values are known as boolean literals.

Collection Literals- Collection literals are List, Tuple, Dic and Set literals.

Special Literals- we use None as a special literal that says field is not created.

4. Operators

Operators are the different symbols that are responsible for performing the operations for operands(values). In Python, we use seven types of operators.

Arithmetic Operators

OperatorFunctionExample
+It perform the addition operationi=5j=6I+j=11
It is used to perform subtraction tasks.i=10j=7i-j=3
*Multiplication taski=3j=4i*j=12
/Divisioni=8j=4i/j=2
%Modulus(gives the remainder after division)i=6j=3i%j=0
**Exponential(power calculation)i=5j=3i**j=5^3
//Floor division(remove decimal value return quotient value)i=5j=2i//j=1

Relational Operators

These are also known as comparison operators as they compare the values. After comparison of the values it returns true or false values.

OperatorFunctionExample
==If operands are equal, truei=5j=5i==jTrue
!=True, when values of operand are not equali=4j=7i!=jtrue
<Less thanTrue,If the value of left operand less than the  value of right operandi=6j=7i<jtrue
>Greater thanTure,If the value of the left operand is greater than the value of the right operand.i=8j=6i>jtrue
<=Less than or equal toTrue,If the value of the left operand is less than or equal to the value of the right operand.i=5j=4i<=jTrue
>=Greater than or equal toTrue, if the value of the left operand is greater than or equal to the value of the right operand.i=5j=7i>=jFalse
<>Not equal toi=10j=15i<>jTrue

Assignment Operators

OperatorFunctionExample
=(Assignment)It assigns value from right side operand to left side operandi= 15It will assign 15 to i
+=(Add then assign)First it performs an addition,then assigns the result to the left hand operand.i+=jthat says i= i+ j
-=(Subtract then assign)After subtraction, it assigns the result to the left operand. i-=jthat says i = i – j
*=(Multiply then assign)The result is assigned to the left operand after multiplication.i*=jthat says i= i * j
/=(Divide then assign)After division, the result is assigned to the left operand.i/=jthat says I = i/ j
%=(Modulus then assign)It assigns the result to the left operand after modulus.i%=jthat says i = i% J
**=(Exponent then assign)Initially performs exponent and then result is assigned to left hand operandi**=jthat says i = i** j
//=(Floor Division then assign)It performs floor division and then result is assigned to left hand operandi//=jthat says i = I // j

Logical Operators

OperatorFunctionExample
and(Logical AND)True,when Both side conditions are true, otherwise false.3<2 and 4<5False
or(Logical OR)True, when at least one condition is true, otherwise false 5<2 or 6<8True
notLogical NOTIt the condition is reverseNot (10>6)False

Membership Operators

Operator    Function
inTrue, When values are in sequence. 
not inTrue, when values are not in sequence.

Bitwise Operators

It performs the operations bit by bit.

For example we have two variables,

i=5

j=10

and their binary values are-

i = 5 = 0101

J = 10 = 1010

OperatorFunctionExample
&(Binary AND)It is 1, when both the bits are 1, otherwise it will be 0i&j0000 0000
|(Binary OR)It is 1, when one of the bits is 1 otherwise 0.I  | j0100 0011
1
^(Binary XOR)It is 0, when both the values are the same,otherwise 1.i^j0101 1100
1
~(Binary Complement)In this we have to interchange the values, if it is 0 make it 1, if it is 1 then make it 0.i~j1001 1101
0110 0010
<<(Binary left shift)Here, Number of bits specified by the right operand moves the left operand to left.i<<j240 Binary value1111 0000
>>(Binary right shift)Left operand is moved to the right by the bits specified by the right operand.i>>j150000 1111

Identity Operators

When we need to compare the memory address of two objects, we use identity operators.

OperatorFunctionExample
isIt shows true, when the identity of  both operands is the same, otherwise false.                  i=10j=10true
Is notTrue, when the identity of both operands is not equal, otherwise false.i=10j=10False

Conclusion

I hope now you know the basics of Python and are ready to step ahead on the way to your success. To learn Python, you do not need to be afraid about your prior knowledge of coding. You can start it as your first programming language then go to a complex one.

In this blog, we have provided you with python fundamentals, which are the building blocks of Python. The plus point is this language is very simple and easy to learn and run. Python has a big role in the data science field, so to excel in this field, learn this valuable language. Get the best python programming homework help.

Frequently Asked Questions

What are the features of Python?

Unlike other languages such as C and its extensions, Python is an interpreted language. It implies that there is no need to compile the program before running it. Other than that, it is dynamically typed, fast and object-oriented.

How much time does it take to learn Python Fundamentals?

It depends on factors such as your interest, grasping speed, environment, and practice. If you do regular practice with dedication, you can clear your basics within one month or less.

How to learn Python?

There are various sources where you can learn this demanding language. You can go for online courses and YouTube tutorials if you want to learn it on your own. Otherwise, you can do any degree or diploma in this field from a college or any university.