Latest Post

 

Strings

Strings in python are surrounded by either single quotation marks, or double quotation marks.

'hello' is the same as "hello".

You can display a string literal with the print() function:

#You can use double or single quotes:


print("Hello")

print('Hello')



Assign String to a Variable

Assigning a string to a variable is done with the variable name followed by an equal sign and the string:

a = "Hello"
print(a)


Multiline Strings

You can assign a multiline string to a variable by using three quotes:


You can use three double quotes:

a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."
""
print(a)


Or three single quotes:

a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'
''
print(a)


-------------------------------

Booleans represent one of two values: True or False.


 AFTER COMPLETION EXCERCISE: https://www.w3schools.com/python/exercise.asp?filename=exercise_datatypes1



Python - Data Types

Python Data Types are used to define the type of a variable. It defines what type of data we are going to store in a variable. The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters.

Python has the following data types built-in by default, in these categories:

Text Type:str
Numeric Types:intfloatcomplex
Sequence Types:
listtuplerange                                            



Data Types in Python
MyVar1 = "Some Text" # This is a text or String print(MyVar1) MyVar2 = 12 # This is an integer print(MyVar2)

Different Data Types in Python

Data TypeTo ChangeDenoted by
Stringstr()” “
Integerint()None
Floatfloat()None
Listslist()[“a”,”b”]
Tuplestuple()(“a”,”b”)
Dictionarydict(name = “somename”,work =”somework”){“a”:”b”,”c”:”d”}
Booleanbool(1)True / False
Setsset((“a”,”b”)){“a”,”b”,”c”}
Data Types in Python
#Different Data Types stored in some variables somestr   = "anyname" someint   = 12 somefloat = 15.25 sometuple = ('abc', 'xyz', 'mno') somedict  = {"abc":"bcd","cde":"def"} somebool  = true someset   = {'abc', 'xyz', 'mno'}

Now let’s dig all the data types one by one.

Strings in Python

Strings Literals

  • To make a string in python you need to include the text between quotes
  • You can Use either single quotes or double quotes. Like of the example given below
Example: Datatypes
# Can use either single or double quotes print("Using double quotes") print('Using single quotes')

Single Line – Strings in Variables

  • You can easily assign string values to variables
  • Create a variable and set its value to a string with using single or multiple quote as discussed earlier
Single Line Strings
# Can use either single or double quotes dblQuoteVar = "Using double quotes" singleQtVar = "Using single quotes" print(dblQuoteVar) print("and") print(singleQtVar)

Multi-Line – Strings in Variables

  • These were the single line strings stored in a variable(s).
  • Now we will How to store multiline strings as variables.
Multi-Line - Strings in Variables
multilineString = """ This is a multiline string This is using three double quotes we can also three single quotes \n """ print(multilineString) # or multiline_single = ''' This is a multiline string This is using three double quotes we can also three single quotes ''' print(multiline_single)

Note: “\n” character is used to give a line break, A new line starts after “\n” and hence its also known as New Line Character.



















----------------------------------------------


Setting the Data Type

In Python, the data type is set when you assign a value to a variable:

ExampleData TypeTry it
x = "Hello World"strTry it »
x = 20intTry it »
x = 20.5floatTry it »
x = 1jcomplexTry it »
x = ["apple", "banana", "cherry"]listTry it »
x = ("apple", "banana", "cherry")tupleTry it »
x = range(6)rangeTry it »
x = {"name" : "John", "age" : 36}dictTry it »
x = {"apple", "banana", "cherry"}setTry it »
x = frozenset({"apple", "banana", "cherry"})frozensetTry it »
x = TrueboolTry it »
x = b"Hello"bytesTry it »
x = bytearray(5)bytearrayTry it »
x = memoryview(bytes(5))memoryviewTry it »
x = NoneNoneTypeTry it »

ADVERTISEMENT

Setting the Specific Data Type

If you want to specify the data type, you can use the following constructor functions:

ExampleData TypeTry it
x = str("Hello World")strTry it »
x = int(20)intTry it »
x = float(20.5)floatTry it »
x = complex(1j)complexTry it »
x = list(("apple", "banana", "cherry"))listTry it »
x = tuple(("apple", "banana", "cherry"))tupleTry it »
x = range(6)rangeTry it »
x = dict(name="John", age=36)dictTry it »
x = set(("apple", "banana", "cherry"))setTry it »
x = frozenset(("apple", "banana", "cherry"))frozensetTry it »
x = bool(5)boolTry it »
x = bytes(5)bytesTry it »
x = bytearray(5)bytearrayTry it »
x = memoryview(bytes(5))memoryviewTry it »








 

https://www.w3schools.com/python/exercise.asp?filename=exercise_variables1

Excercise


Python Variable Names

Every Python variable should have a unique name like a, b, c. A variable name can be meaningful like color, age, name etc. There are certain rules which should be taken care while naming a Python variable:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number or any special character like $, (, * % etc.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Python variable names are case-sensitive which means Name and NAME are two different variables in Python.
  • Python reserved keywords cannot be used naming the variable.

Example

Following are valid Python variable names:

counter = 100 _count = 100 name1 = "Zara" name2 = "Nuha" Age = 20 zara_salary = 100000 myvar = "John" my_var = "John" _my_var = "John" myVar = "John" MYVAR = "John" myvar2 = "John" print (counter) print (_count) print (name1) print (name2) print (Age) print (zara_salary)

This will produce the following result:

100
100
Zara
Nuha
20
100000

Example

Following are invalid Python variable names:

1counter = 100 $_count = 100 zara-salary = 100000 print (1counter) print ($count) print (zara-salary)

This will produce the following result:

File "main.py", line 3
    1counter = 100
           ^
SyntaxError: invalid syntax

P


Multi Words Variable Names

Variable names with more than one word can be difficult to read.

There are several techniques you can use to make them more readable:

Camel Case

Each word, except the first, starts with a capital letter:

myVariableName = "John"

Pascal Case

Each word starts with a capital letter:

MyVariableName = "John"

Snake Case

Each word is separated by an underscore character:

my_variable_name = "John"
MYPAGE
---------------------------------------------------------------------------

Python Variables - Assign Multiple Values

x, y, z = "Orange""Banana""Cherry"
print(x)
print(y)
print(z)

One Value to Multiple Variables

And you can assign the same value to multiple variables in one line:

x = y = z = "Orange"
print(x)
print(y)
print(z)


Python - Output Variables

Output Variables

The Python print() function is often used to output variables.

x = "Python is awesome"
print(x)

In the print() function, you output multiple variables, separated by a comma:

x = "Python"
y = "is"
z = "awesome"
print(x, y, z)

You can also use the + operator to output multiple variables:

x = "Python "
y = "is "
z = "awesome"
print(x + y + z)


For numbers, the + character works as a mathematical operator:

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


In the print() function, when you try to combine a string and a number with the + operator, Python will give you an error:

x = 5
y = "John"
print(x + y)
The best way to output multiple variables in the print() function is to separate them with commas, which even support different data types:
x = 5
y = "John"
print(x, y)


MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget