0% found this document useful (0 votes)
1 views6 pages

Basics in Python

The document explains the use of comments in Python for code readability and preventing execution during testing. It also covers built-in data types in Python, how to get a data type using the type() function, and the concept of casting variables to specific types. Additionally, it discusses strings, including their representation, usage of quotes, and how to assign multiline strings to variables.

Uploaded by

arivazhagi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Basics in Python

The document explains the use of comments in Python for code readability and preventing execution during testing. It also covers built-in data types in Python, how to get a data type using the type() function, and the concept of casting variables to specific types. Additionally, it discusses strings, including their representation, usage of quotes, and how to assign multiline strings to variables.

Uploaded by

arivazhagi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Comments

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when


testing code.

Creating a Comment

Comments starts with a #, and Python will ignore them:

ExampleGet your own Python Server


#This is a comment
print("Hello, World!")

Comments can be placed at the end of a line, and Python


will ignore the rest of the line:

Example
print("Hello, World!") #This is a comment

A comment does not have to be text that explains the


code, it can also be used to prevent Python from
executing code:

Example
#print("Hello, World!")
print("Cheers, Mate!")
Python Data Types

Built-in Data Types

In programming, data type is an important concept.

Variables can store data of different types, and different


types can do different things.

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


these categories:

Text Type: str


Numeric int, float, complex
Types:
Sequence list, tuple, range
Types:
Mapping dict
Type:
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType

Getting the Data Type


You can get the data type of any object by using
the type() function:

ExampleGet your own Python Server


Print the data type of the variable x:
x = 5
print(type(x))

Python Casting

Specify a Variable Type

There may be times when you want to specify a type on


to a variable. This can be done with casting. Python is an
object-orientated language, and as such it uses classes to
define data types, including its primitive types.

Casting in python is therefore done using constructor


functions:

 int() - constructs an integer number from an


integer literal, a float literal (by removing all
decimals), or a string literal (providing the string
represents a whole number)
 float() - constructs a float number from an integer
literal, a float literal or a string literal (providing the
string represents a float or an integer)
 str() - constructs a string from a wide variety of
data types, including strings, integer literals and float
literals
ExampleGet your own Python Server
Integers:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

Example
Floats:
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2

Example
Strings:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'

Python Strings

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:

ExampleGet your own Python Server


print("Hello")
print('Hello')

Quotes Inside Quotes


You can use quotes inside a string, as long as they don't match the quotes
surrounding the string:

Example
print("It's alright")
print("He is called 'Johnny'")
print('He is called "Johnny"')

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:

Example
a = "Hello"
print(a)

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

Example
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:

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

You might also like