0% found this document useful (0 votes)
8 views

Python Intro (Ymca)

The document provides an introduction to the Python programming language including what it is, what it can be used for, why it is popular, its features and syntax. It also covers Python variables, data types, operators and introduces some basic concepts like strings, lists, loops etc.

Uploaded by

Shubham Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Intro (Ymca)

The document provides an introduction to the Python programming language including what it is, what it can be used for, why it is popular, its features and syntax. It also covers Python variables, data types, operators and introduces some basic concepts like strings, lists, loops etc.

Uploaded by

Shubham Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Python Introduction

Rohan saini
Mtech-
CSE
Python Introduction

What is Python?
• Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
• It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.

What can Python do?


• Python can be used on a server to create web applications.
• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify files.
• Python can be used to handle big data and perform complex mathematics.
• Python can be used for rapid prototyping, or for production-ready software
Why Python?
• Python works on different platforms (Windows, Mac, Linux, Raspberry Pi,
etc).
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs with fewer lines
than some other programming languages.
• Python runs on an interpreter system, meaning that code can be executed
as soon as it is written. This means that prototyping can be very quick.
• Python can be treated in a procedural way, an object-oriented way or a
functional way.
Good to know
• The most recent major version of Python is Python 3.
• Python was designed for readability, and has some similarities to the English
language with influence from mathematics.
• Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
• Python relies on indentation, using whitespace, to define scope; such as the
scope of loops, functions and classes. Other programming languages often
Python IDE

Your data science toolkit


With over 20 million users worldwide, the open-source Individual Edition (Distribution) is
the easiest way to perform Python/R data science and machine learning on a single
machine. Developed for solo practitioners, it is the toolkit that equips you to work with
thousands of open-source packages and libraries.
Why Anaconda?

Installation link: https://www.anaconda.com/products/individual#Downloads


Let’s get started
print("Hello, World!")

Python Variables:
a = 55
b = "Hello, World!“

Python Comments:
#This is a comment.
print("Hello, World!")
• Since Python will ignore string literals that are not assigned to a
variable, you can add a multiline string (triple quotes) in your code,
and place your comment Inside it:
• Eg.
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")

Creating variables:
a = 10 # x is of type int
a = “Rohan" # x is now of type str
print(a)
• Type Casting
a = str(5) # a will be ‘5'
b = int(5) # b will be 5
c= float(5) # c will be 5.0

q=5
w = “Rohan"
print(type(q)) #checking the data type
print(type(w))

Case sensitive
a=4
A = "Sally"
#A will not overwrite a
Variable Names
• A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume). Rules for Python variables:A
variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three
different variables)

Assigning values to multiple variable


a, b, c = “TATA", “MAHINDRA", “PRAVAIG "
print(a)
print(b)
print(c)
Assigning single value to multiple variable
x = y = z = "India"
print(x)
print(y)
print(z)

To combine both text and a variable, Python uses the + character:


Eg.
x = “a programming language"
print("Python is " + x)
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 Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set
Boolean Type: bool
Example Data Type

x = "Hello World" str

x = 20 int

x = 20.5 float

x = 1j complex

x = ["apple", "banana", "cherry"] list

x = ("apple", "banana", "cherry") tuple

x = range(6) range

x = {"name" : "John", "age" : 36} dict

x = {"apple", "banana", "cherry"} set

x = True bool
There are three numeric types in Python:
• int
• float
• complex
Eg.
x = 5 # int
y = 6.8 # float
z = 2j # complex

Eg. for complex numbers


x = 3+5j
y = 5j
z = -5j

print(type(x))
print(type(y))
print(type(z))
Python Casting
Casting in python is therefore done using constructor functions:

• int() - constructs an integer number from an integer literal, a float


literal (by rounding down to the previous whole number), 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
Integers:
• x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

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

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

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


double quotation marks.
• 'hello' is the same as "hello".

Assign String to a Variable


Eg.
• a = "Hello"
print(a)
Multiline Strings
Eg. a = """You can assign a multiline string to a variable by using three
quotes."""
print(a)
• Like many other popular programming languages, strings in Python
are arrays of bytes representing unicode characters.
• However, Python does not have a character data type, a single
character is simply a string with a length of 1.
• Square brackets can be used to access elements of the string.
• Example
• Get the character at position 1 (remember that the first character has
the position 0):
Eg.
a = "Hello, World!"
print(a[1])
Looping Through a String
• for x in “YMCA":
print(x)
The len() function returns the length of a string:
• a = "Hello, World!"
print(len(a))

Check if “positive" is present in the following text:


• txt = "With a positive attitude, you can be anything you want to be."
print(“positive" in txt)

Check if "expensive" is NOT present in the following text:


• txt = "The best things in life are free!"
print("expensive" not in txt)
Slicing
• You can return a range of characters by using the slice syntax.
• Specify the start index and the end index, separated by a
colon, to return a part of the string.
Eg. a = "Hello, World!"
print(a[2:5])
Get the characters from the start to position 5 (not included):
Eg. a = "Hello, World!"
print(a[:5])
Get the characters from position 2, and all the way to the end:
Eg. a = "Hello, World!"
print(a[2:])
Use negative indexes to start the slice from the end of the string:
Eg. b = "Hello, World!"
print(b[-5:-2])
The upper() method returns the string in upper case:
• a = "Hello, World!"
print(a.upper())
The lower() method returns the string in lower case:
• a = "Hello, World!"
print(a.lower())
The strip() method removes any whitespace from the beginning or
the end:
• a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
The replace() method replaces a string with another string:
• a = "Hello, World!"
print(a.replace("H", "J"))
Merge variable a with variable b into variable c:
• a = "Hello"
b = "World"
c=a+b
print(c)
we cannot combine strings and numbers like this:
• age = 36
txt = "My name is John, I am " + age
print(txt)
• Note: above example will create error
The format() method takes the passed arguments, formats
them, and places them in the string where the
placeholders {} are:
• Use the format() method to insert numbers into strings:
• age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
• quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
You can use index numbers {0} to be sure the
arguments are placed in the correct placeholders:
• quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))

You might also like