Education logo

Python Basics

Part-2

By PRANSHU JAINPublished 3 years ago 3 min read
Python Basics
Photo by Clément Hélardot on Unsplash

So this is the second part of our Python Journey, If you guys want to see my previous posts you can go through them by using this link.

So let’s continue towards the second part, in which we will start from understanding assignment statements.

Assignment Statements

We assign a value to a variable using the assignment statement(=). An assignment statement consists of an expression on the right-hand side & a variable to store the result. For example:

  • x = 2 ← Assignment Statement
  • x = x+2 ← Assignment with expression
  • print(x) ← Print statement

Note: Here x is a variable, 2 is a constant, = is a operator, and print() is a function.

Numeric Expressions

  • Because of the lack of mathematical symbols on computer keyboards — We use “computer speak” to express the classic math operations.
  • Asterisk is multiplication.
  • Exponentiation (raise to a power) looks different from math.

Operator → Operation

  • + → Addition: adds two operands
  • — → Subtraction: subtracts two operands
  • * → Multiplication: multiplies two operands
  • / → Division: Divides the first operand by the second, but the result will be of float data type.
  • ** →Power: Returns first raised to power second
  • % → Modulus: returns the remainder when the first operand is divided by the second
  • // → Division divides the first operand by the second, but the result will be of float type, which means it will give the number before the decimal point and remove the numbers after the decimal point.

Order of Evaluation

When we use string operator together, Python must know which one to-do first. This is called “Operator Precedence.”

Which operator takes precedence over the others?

This could be answered by the following Operator Precedence rules:

  1. Parentheses are always respected.
  2. Exponentiation (raise to a power).
  3. Multiplication, division, and remainder.
  4. Addition & Subtraction.
  5. Left to Right.

What does the word ‘Type’ mean?

  • In Python variables, literals & constants have a “type”.
  • Python knows the difference between an integer number and a string.
  • For example, “+” means “addition” if something is a number & “concatenate” if something is a string.

Type Matters!

  • Python knows what “type” of everything which we are writing in Python.
  • Some operations are prohibited, as you cannot add “1” to a string.
  • The “type()” function can tell us what type something is in Python.
  • For more interaction with type function, you can go through this link and run the following code for output.

demo = 'hello' + 'world!'

print(type(demo))

Two types of numbers

  • Integers are whole numbers. For example: -14, -2, 0, 15, 1233,etc.
  • Floating point numbers have decimal part. For example: -2.5, 0.0, 3.14, 78.6, etc.

Type conversions

  • When you put an integer & floating point in an expression, the integer is implicitly converted to a float type.
  • You can control this by using built-in functions like “int()” & “float()”.
  • For demonstration of type conversion, follow this link to run the following code.

print(float(99)+100)

i=42

print(type(i))

f = float(i)

print(f)

print(type(f))

  • You can also use float() and int() functions to convert between strings and integers.
  • You will get an error if the string does not contain numeric characters.
  • Go through this link for demonstrating following code yourself.

# If you will uncomment the commented commands, then you will get error.

sval = '123'

print(type(sval))

# print(sval+ 1)

ival = int(sval)

print(type(ival))

print(ival + 1)

nsv = 'hello guys!'

# niv = int(nsv)

Please follow and like my post and stay tuned for exciting new posts!!

My profiles : LinkedIn | GitHub

Reposting here as I posted it before on Medium.

Please do subscribe for more.

collegecoursesdegreehow tostudentteacherhigh school

About the Creator

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.