Python Variables

Variables are used to store values.To create a variable, use the equal sign (=).

In this example, we have a variable named fruit and we assign the "mangos" value to it.



Python Variables



color = "green"


Now, we can use our variable:


print(color)


Here is another example:


name = "Rahul"

print("Hello" + name)



Changing a Variable Value


To change the value of a variable, simply assign a new value to it.


name = "Rahul"

name = "Amit"

print("Hello + name)


You can also assign a variable to a value of another type.

In this example, the variable is originally a string (text) then we'll change its value to a number.


name = "Rahul"

print(name)


Rules for Naming Variables


• A variable name should start with a letter or underscore (_).

• A variable name cannot start with a number.

• A variable name is case-sensitive.

• A variable name should only contain underscores ( ) and alphanumeric characters.


Here are some examples of legal variable names:


myname="Rahul"

my_name = "Rahul"

_myname = "Rahul"

myname1 = "Rahul"

myName = "Rahul"


Here are some examples of illegal variable names:


$myname = "Rahul"

my name = "Rahul"

1myname = "Rahul"

my-name = "Rahul"


Long Variable Names


When naming a variable with a long name, use the underscores to separate words.


Here is an example:


my_favorite_fruit = "Banana" 

print(my_favorite_fruit)


You can also use the camelCase format where every first letter of the words are capitalized.


myFavorite Fruit = "Banana"

 print(myFavoriteFruit)



Python Data Types



In this tutorial, you will learn the most basic data types in Python.

Numbers


In Python, there are two basic types of numbers and they are called integer and floating point numbers.
An integer does not have decimals.


#x is an integer 
x = 33
print(x)


A floating point number has decimals.


# x is a floating point number 
x = 33.33
print(x)

To add numbers, use the plus sign (
,)


x = 5
y = 8
print(x + y)

Strings


Strings are simply text. A string must be surrounded by single quotes or double quotes.


# using single quotes

fruit = 'mangos' print(fruit)

Try It Yourself

# using double quotes

fruit = "mangos" print(fruit)



Single or double quotes?


Use single quotes when your string contains double quotes.


x = 'I Love HTML' ("I Love Python")
 print(x)


Use double quotes when your string contains single quotes.


x = "I love Python!" 
print(x)

Booleans


The Boolean data type can only have one of these two values: True or False.


bool = True
print(bool)
bool = False
print(bool)


When comparing two values, Python returns a Boolean. Let's compare some numbers and see whether the expression evaluates to True or False.


print(9 > 4) # True
print(9 > 4) # True
print(3< 2) # False 
print(6 == 6) # True
print(8 == 5) # False

Lists


  • A list is an ordered collection of data.
  • It can contain strings, numbers etc.
  • Lists are written with square brackets ([]).
  • The values (also called elements) in a list are separated with commas ( ,).

list = ["Kivy", "Bananas", "Mangos",]
print(list)
list = [2, 4, 6, 8, 10] 
print(list)

Python Numbers


In Python, there are three types of numbers:

• Integer int
• Floating Point float
• Complex - Complex

Integer


An integer is a number without decimals.


a = 3
b = 5
c = 6

Floating Point


A floating point number or a float is a number with decimals.


a = 5.0
b = 8.21
c = 76.25

Complex


A complex number is an imaginary number. To yield a complex number, simply append a 'j' or 'J' to a numeric value.


a = 7j
b = 7.81j
c = 3.93j

Getting the Type of Numerical Values


We can get the specific data type of numbers using the type() function.


a = 6
b = 86
c = 7.24

print(type(a))
print(type(b))
print(type(c))

Adding Numbers


To add numbers, use the plus sign (,)


x = 8+ 4
print(x)

Subtracting Numbers


To subtract numbers, use the minus sign (-).


x = 57
print(x)

Multiplying Numbers


To multiply numbers, use the asterisk sign (*).


x = 2 * 6
print(x)

Dividing Numbers


To divide numbers, use forward slash (/).


x = 4/2 print(x)

Python Number Methods

In this lesson, we'll study some useful methods or functions that can be used to work with numbers.


Rounding a Number


The round() function rounds a floating point number to a specified decimal point.


Syntax:


round (number, ndigits)


number the number to be rounded off ndigits - the number of decimal places.


pi= 3.1415926535897 # this is 1
two_decimals = round(pi, 2)
three_decimals = round(pi, 3)
print(two_decimals)
print(three_decimals)


Raising a Number to a Power


The pow() function is used to raise a number to a specified power.


Syntax:


pow(base, exp)


base the base number exp the exponent.


x = pow(4, 2)
y = pow(4, 3)

print(x)
print(y)


Getting Absolute Value


The abs() function returns the absolute value of a number.What is the absolute value? In mathematics, the absolute value is the non-negative value of a number.

For example, the absolute value of 21 is 21 and the absolute value of -8 is 8.


a = abs(-2)
b = abs(-3.21)
print(a)
print(b)


Getting Quotient and Remainder


The divmod() function takes two numbers and return a pair of numbers consisting of their quotient and remainder.In this example, 5 is divided by 2

The divmod() function takes two numbers and return a pair of numbers consisting of their quotient and remainder.

In this example, 5 is divided by 2 therefore the quotient is 2 and the remainder is 1.

\

x = divmod(5, 2)
print(x)


Python Strings


Strings in Python, are sequences of characters or simply text. A string can either be created using single quotes or double quotes.


Using single quotes:


str = 'Hello World!' 

print(str)


Using double quotes:


str = "Hello World!" 

print(str)


Single or Double Quotes


It does not really matter which one you use because Python treat them the same way.

However, if your string contains double quotes use single quotes.


text = 'He said "I love Python!' 

print(text)


And, if your string contains single quotes use double quotes.


str= "I Love Python"

 print(str)


Multi-Line Strings


In Python, it is possible to have a string that spans multiple lines.

To create a multiline string, surround a string with triple single quotes or triple double quotes.


Using triple single quotes:


str = '''Python is fun,I love Python learning Python.'''

print(str)


Using triple double quotes:


str= """Python is fun,I love Python learning Python"""

print(text)


Concatenating Strings


Concatenating strings simply means combining or adding strings together.

To combine strings, use the plus sign (+).


a = "Hello"

b = "World!"

str = a + b

print(str)


We can combine as many strings as we want.


a = "I Love Python"

b = "I love HTML"

c = "I love JavaScript"

d = " I love CSS"

str = a + b + c + d

print(str)



Escaping Characters in a String


Escaping characters is important in handling strings. It helps us make sure that our strings are recognized as pieces of text, and not as part of the code.

In this example, there is a single quote inside a string created with single quotes because of that it produces an error.


# This will produce a syntax

str = 'I love Python' 

print(str)


We can prevent this error by escaping the single quote.

To escape a character, put a backslash (\) right before the character, which in this case is the single quote.


# this will NOT produce a syntax

 str = 'Let\'s learn Python'

print(str)


Accessing Parts of a String


In Python, we can access a single character or a range of characters from a string.


Indexing


To access a single character, use indexing. Indexing uses square brackets ([]) to access characters.

O represents the first character, 1 represents the second character, and so on.


str = "Hi everyone"
print(str[0])
print(str[1])


While -1 represents the last character, and -2 represents the second to the last character.


str = "Hi everyone"
print(str[-1])
print(str[-2])


Slicing


To access a range of characters, use slicing.


Slicing also uses square brackets ( []).


The square brackets can contain two integers separated by a colon (: ). The first integer is the start index, the second integer is the end index (exclusive).


str = "Love Python"

print(str[2:5])


Even though index 5 represents the letter n in our example above, it was still not printed because when slicing the end index is not included.

To slice from a specific position until the end of the string don't specify the second integer.


str = "I love Python"

print(str[2:])


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.