0% found this document useful (0 votes)
17 views23 pages

Pyhton Foundation 1

The document provides an introduction to Python programming, covering fundamental concepts such as the print function, comments, variables, data types, and operators. It includes exercises for practical application, guiding users to write code snippets and understand Python's syntax and functionality. Additionally, it explains the differences between data structures like lists, tuples, and dictionaries, as well as various operators used in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views23 pages

Pyhton Foundation 1

The document provides an introduction to Python programming, covering fundamental concepts such as the print function, comments, variables, data types, and operators. It includes exercises for practical application, guiding users to write code snippets and understand Python's syntax and functionality. Additionally, it explains the differences between data structures like lists, tuples, and dictionaries, as well as various operators used in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Hello World

• print function is used to print the content inside the quotes


• can be in 'single quotes' or "double quotes"
print("Hello World")

What just happened?


• when you run Python code
– the computer interprets the code and executes it
• print is a function which will display whatever is provided in brackets
– in case of a mathematical expression, the result is
evaluated and displayed
– if there is a string, it is simply displayed
• there are many such functions in Python built for specifc tasks
– simply call the function and give it an input
• type is another such function
– it displays the type of the data provided in brackets Alright!

Let's see you run a python code now ,

Exercise : 1
Write a print function that prints "I am new to python!"

Help Command
• shows the description about the object that is given to the command
• lets see how the help() command works for the print() command

help(print)

Comments
• comments are text in code that is not executed by the interpreter
– in Python, comments begin with #
• comments may be used creatively to comment out pieces of code to
stop the interpreter from running it
© 2024 TECHBLUME. ALL RIGHTS RESERVED.
• shortcut to toggle comment on a line – CMD + / (macOS)
– Ctrl + / (Windows)

# this is a single comment

print('this is not a comment')

# comments can be added like this as well


"""
This is a
multi-line comment
"""
print("this is not a comment either")

Now, let's try to write our own comments

Exercise : 2
To the program where you printed out, I am Learning python

Add a comment It is possible to write within my code blocks without


python using #

Variables

Variables are the data entities that takes a value.


# bind number 7 to variable 'x'

x = 7 # the value of 7 is assigned to x

# print value of 'x'


print(x)

Python Variable Naming Convention (Industry Standard)


• variables names start with a lowercase letter or underscore
– can be followed by numbers or underscore
© 2024 TECHBLUME. ALL RIGHTS RESERVED.
• snake_case is the conventional way of writing variable names with
multiple words
• variable names cannot begin with a number but numbers can be a
part of the variable name later
- eg: names like: va923, _app4r_, f8_9, tmp; are allowed
- eg: names like: 9va23, #app4r_, f8-9, tm*p; are NOT allowed

Let's try making a variable that contains your name then print it !

Exercise : 3

Create a variable as called age and add your age to it Next print out your
age in your notebook !

Simple Program
• input: uses the input command
• output: used the print command

name = input('What is your name? ')

print('Welcome to the world of Python,', name )

Let's create input function that not only takes the name of the user but also
their age and then let's print it

Exercise : 4
Create two inputs with one taking the name and the other taking the age

And then print out Hey <name> I am glad you started learning python at
the age of <age>

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


Data Types
• a particular kind of data entity, defined by the values it can take

• Python has several built-in data types, some of them are:

– numeric data types


– text sequence data type
– sequence data types
– mapping data type
– boolean data type
– set data type
• the type() function is used to check the data type of something in
Python

Numeric Data Types


•two basic numeric data types
a. int: integers
b. float: decimal numbers

# create int type variable

x = 7 # 7 is an integer

# check type of variable

type(x)

# create float type variable


x = 3.14 # 3.14 is a decimal number with a characteristic and mantissa
# check type of variable

type(x)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


Text Sequence Types
• strings are a text sequence type
– they are a sequence of characters
– they are immutable
– they are ordered

# define a string
python_string = 'TECHBLUME is a awesome Bootcamp!'

# check type
type(python_string)

Now that we know how to find out the datatype of variables

Exercise : 5

What is the datatype of these three variables :

• discount = 43.8
• height = 143
• address = '#301, Pelican Street'

Sequence Types

• there are three kinds of sequence


types:

– list: mutable sequence

– tuple: immutable sequence


– range: immutable sequence

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


List

# define a list

list_example = [ '3' , 92 , 'x' , 3.14 ]

print(list_example)

# check length of the list

print(len(list_example))

# check type
type(list_example)

Tuple

# define a tuple

tuple_example = (5,'11','a',2.86)

print(tuple_example)

# check length of tuple

print(len(tuple_example))

# check type

type(tuple_example)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


List v/s Tuple

Lists Tuples
 ordered group of comma  ordered group of comma
separated values in square separated values in parenthesis
brackets
 square brackets are mandatory  parenthesis not mandatory
 mutable  immutable
 once created, content changes  once created, content cannot be
can be made changed
 should be chosen if content is  chosen if content is fixed and
not fixed, and keeps changing never changes
 cannot be used as keys for  can be used as keys for
dictionaries dictionaries
 are not hashable and  are hashable and immutable
immutable

Let's make something using lists!

Exercise : 6

Make a list of your favourite colour, food and place and save it in a list
called my_favs

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


Range

# define a range

range_example = range(7)

print(range_example)

# check length of the range

print(len(range_example))

# check type

type(range_example)

# define range with (start,stop,step)

range_example = range(0,20,2)

print(range_example)

# check length of the range

print(len(range_example))

# check type

type(range_example)

Let's get a bit crafty with the range function

Exercise : 7

Using the range function from 4 to 40 and then check the length

Mapping Types
• dictionary is a mapping data type
– it is mutable
– unordered

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


# define a dictionary
b = {'one': 1, 'two': 2, 'three': 3}

print(b)

# check type
type(b)
What can we do with dictionaries?

Exercise : 8

Make a dictionary with students name and their scores !

• Student : Jake Score : 70


• Student : Mako Score : 75
• Student : Alex Score : 78.5

Boolean Types
• state of truth
–may be True or False

# define a 'true' boolean

boolean = True

# check type

type(boolean)

# define a 'false' boolean

boolean = False

# check type

type(boolean)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


Set Types
• set is an unordered collection of distinct objects

set_of_numbers = {0,9,1,5,7}

print(set_of_numbers)

names = ['olivia', 'caleb', 'olivia']

names_set = set(names)

unique_names = list(names_set)

print(unique_names)

Operators
• popular kinds of operators
– numeric operators
– comparison operators
– logical operators
– assignment operators
– identity operators
– membership operators
– bitwise operators
• some are discussed below

Numeric Operators
•used with numeric data types to perform mathematical operations
– +: addition
– -: subtraction
– *: multiplication
– /: division
– //: floor division

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


– %: modulus (remainder)
– **: raising to power
– abs(): absolute value of number
– round(): rounds a number
– bin(): get binary version of a number
– hex(): get hexadecimal version of a number

# set values of x, y and z for numeric operators exploration

x = 5 # integer value

y = 2.0 # float value

z = -13 # negative integer

#(run this cell to initialize values for the next few cells!)

# sum of x and y

print(x + y)

# difference of x and y
print(x - y)
# product of x and y

print(x * y)

# quotient of x and y

print(x / y)

# floored quotient of x and y

print(x // y)

# remainder of x / y

print(x % y)

# absolute value

print(abs(z))

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


# x raised to power y

print(x ** y)

# rounding a decimal example - round down


print(round(2.1))

# rounding a decimal example - round up

print(round(3.7))

# binary version of the number

print(bin(37))

# hexadecimal version of the number


print(hex(37))

Operators are the lifelines of any programming language, let’s put them

to good use

Exercise : 9

Create a variable with the value of pi as 3.14, create a new variable as


radius and assign it a float value, this will be the radius of a circle and then
output the area of the circle using the formula **pi*radius^2**

Comparison Operators
•used to compare two values
– <: less than
– >: greater than
– <=: less than or equal to
– >=: greater than or equal to
– ==: equality
– !=: inequality

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


# Greater Than or Equal

i=3

j=7

print(i > j)

# Greater Than or Equal

i=3

j=7

print(i >= j)

# Less Than

i=3

j=7

print(i < j)

# Less Than or Equal

i=3

j=7

print(i <= j)

# Equality Test #1

i=3

j=7

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


print(i == j)

# Equality Test #2

i=3

j=3

print(i == j)

# Inequality Test #1

i=3

j=7

print(i != j)

# Inequality Test #2

i=3

j=3

print(i != j)

# Greater Than or Equal

i=3

j=7

print(i > j)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


# Greater Than or Equal

i=3

j=7

print(i >= j)

# Less Than

i=3

j=7

print(i < j)

# Less Than or Equal

i=3

j=7

print(i <= j)

# Equality Test #1

i=3

j=7

print(i == j)

# Equality Test #2

i=3

j=3

print(i == j)

# Inequality Test #1

i=3

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


j=7

print(i != j)

# Inequality Test #2

i=3

j=3

print(i != j)

Let's see what comparison operator can do for us

Exercise : 10

In the last exercise you created a program to get the area of a circle ,
create a print function that checks if the area that your program created is
bigger than 50 or not

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


Logic Operators
•used to combine conditional statements
– and
– or
– not

not Operator

# ‘not’ operator

a = True

b = False

print( not a)

print( not b)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


and Operator

# a and b are true

a = True

b = True

print( a and b)

# only a is true

a = True

b = False

print( a and b)

# only b is true

a = False

b = True

print( a and b)

# a and b both are false

a = False

b = False

print( a and b)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


or Operator

# a and b are true

a = True

b = True

print( a or b)

# only a is true

a = True

b = False

print( a or b)

# only b is true

a = False

b = True

print( a or b)

# a and b both are false

a = False

b = False

print( a or b)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


To be OR not to be ? Let's explore what we can get done using logical

operators

Exercise : 11

Create a print function for our circle area generator to check if the area is
between 20 AND 50

Assignment Operators
• used to assign values to variables
–=
• following operators combine numerical operations at the same time
as assignment
– +=
– -=
– *=
– /=
– //=
– %=
– **=

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


# assignment operator

x = 21

print(x)

# += operator

x += 3

print(x)

# -= operator

x -= 3

print(x)

# *= operator

x *= 3

print(x)

# /= operator

x /= 3

print(x)

# //= operator

x //= 3

print(x)

# %= operator

x %= 3
print(x)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


Identity Operators
• used to compare objects
 not if they are equal
 but if they are actually the same object with the same memory
location
• two identity operators:
– is
– is not

# define list

x = [“apple” , “banana” ]

y = [“apple” , “banana” ]

z=x

# ‘is’ test

print( x is z)

# ‘is’ test

print( x is y)

# equality test

print( x == y)

# ‘is not’ test

print( x is not y)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.


Membership Operators
• used to test if a sequence has a specified object
• two membership operators:
– in
– not in

# define list

x = [“apple” , “banana” ]

# ‘in’ test

print(“banana” in x)

# ‘in’ test

print(“pineapple” in x)

# ‘not in’ test

print(“pineapple” not in x)

© 2024 TECHBLUME. ALL RIGHTS RESERVED.

You might also like