Sunday, August 15, 2021

Python -Booleans and variables

 Python -Booleans and variables

What is a Variable?


VARIABLES are entities that help us store information and retrieve it later.

  • A variable with a fixed name can store information of nature like numeric, textual, boolean, etc.
  • A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.
  • The type of data contained in a variable can be changed at the user's will.

Example:

You can store numbers in variables.
The standard rule is you write the variable name followed by = sign and the value it will take

x = 5
y = 3


Basic Arithmetic operations we can do on x and y. Later we will be doing operations on thousands of such numbers in one go!


z= x + y

ans : 5+3 = 8


Floats:


Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place.

Example:

1.0 , 0.1 . 0.6 1.36 etc.


Integers:


An integer (from the Latin integer meaning "whole") is colloquially defined as a number that can be written without a fractional component

Example:

21, 4, 0, and −2048 are integers, while 9.75, 512, and √2 are not integers.


Rules for naming a variable in Python:


  • Variables names must start with a letter or an underscore like the product, product
  • The remainder of your variable name may consist of letters, numbers, and underscores
  • spacy1, pyThon, machine learning is some valid variable names
  • Names are case-sensitive.
  • case_sensitive, CASE SENSITIVE, and Case Sensitive are each a different variable.
  • Names cannot begin with a number. Python will throw an error when you try to do so
  • Names can not contain spaces, use instead names can not contain any of these symbols:
  • :'",<>/?|\!@#%^&*~-+
  • It is considered best practice that names are lowercase with underscores
  • Avoid using Python built-in keywords like liststr , def etc. 


We will talk more about such conventions later on.

Boolean Variables:


  • A Boolean variable only takes two values either True or False. It is used for comparisons

Comparison Operators:.


  • These operators will allow us to compare variables and output a Boolean value (True or False).
  • If you have any sort of background in Math, these operators should be very straightforward.
  • First, we'll present a table of the comparison operators and then work through some examples:
  • In the table below, a=3 and b=4.
OperatorDescriptionExample
==If the values of two operands are equal, then the condition becomes true.(a == b) is not true.
!=If the values of two operands are not equal, then the condition becomes true.(a != b) is true
>If the value of the left operand is greater than the value of the right operand, then the condition becomes true.(a > b) is not true.
<If the value of the left operand is less than the value of the right operand, then the condition becomes true.(a < b) is true.
>=If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes true.(a >= b) is not true.
<=If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes true.(a <= b) is true.


  • Python comes with Booleans (with predefined True and False displays that are basically just the integers 1 and 0). It also has a placeholder object called None. Let's walk through a few quick examples of Booleans (we will dive deeper into them later in this course).


Thanks for reading...

You can learn more using the below links





*** Comment for related notes I can provide to you notes in pdf. ***




No comments:

Post a Comment

Python - Conditional statements ,if condition in python

 Python - if - Conditional statements What is the if condition in python? Decision-making is the anticipation of conditions occurring while ...