Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Core Python Part 1

This document contains examples of Python code demonstrating various programming concepts like variables, data types, functions, conditionals, loops, strings, lists, dictionaries and more. The code samples are accompanied by explanations and output examples.

Uploaded by

Chanchal jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Core Python Part 1

This document contains examples of Python code demonstrating various programming concepts like variables, data types, functions, conditionals, loops, strings, lists, dictionaries and more. The code samples are accompanied by explanations and output examples.

Uploaded by

Chanchal jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Corey_Python_1.

ipynb - Colab 10/05/24, 6:35 PM

message = "Sam\'s World"


print("Great " + message)

Great Sam's World

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 1 of 10
Corey_Python_1.ipynb - Colab 10/05/24, 6:35 PM

# # Define a variable of type list


# import typing
# list_of_numbers: typing.List[int] = [1, 2, 3]
# # Define a variable of type str
# z: str = "Hello, world!"
# # Define a variable of type int
# x: int = 10
# # Define a variable of type float
# y: float = 1.23
# # Define a variable of type list
# list_of_numbers: typing.List[int] = [1, 2, 3]
# # Define a variable of type tuple
# tuple_of_numbers: typing.Tuple[int, int, int] = (1, 2, 3)
# # Define a variable of type dict
# dictionary: typing.Dict[str, int] = {"key1": 1, "key2": 2}
# # Define a variable of type set
# set_of_numbers: typing.Set[int] = {1, 2, 3}

import typing
a:int = 1
b:str = "Hello World"
c: float = 2.0
d:typing.List[int] = [1, 2, 3]
e:typing.Tuple[int, int, int] = (4, 5, 6)
f: typing.Dict[str, int] = {"k1": 1, "k2": 2}
g: typing.Set[int] = {7, 8, 9}

print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)

 1
Hello World
2.0
[1, 2, 3]
(4, 5, 6)
{'k1': 1, 'k2': 2}
{8, 9, 7}

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 2 of 10
Corey_Python_1.ipynb - Colab 10/05/24, 6:35 PM

def greeting(name, department):


print("Greetings", name, "- Department is", department)

greeting("Kay", "HR")
greeting("Cameron", "Finance")

Greetings Kay - Department is HR


Greetings Cameron - Department is Finance

a_list = [4, 6, 8, 1, 2, 7, 3, 5]
s_list = sorted(a_list)
print(a_list)
print(s_list)
print(max(a_list))
print(min(a_list))

[4, 6, 8, 1, 2, 7, 3, 5]
[1, 2, 3, 4, 5, 6, 7, 8]
8
1

a_list = ["abd", "abc", 'a', "zaq", "adz", "abz"]


s_list = sorted(a_list)
print(a_list)
print(s_list)

['abd', 'abc', 'a', 'zaq', 'adz', 'abz']


['a', 'abc', 'abd', 'abz', 'adz', 'zaq']

print(not "A" == False)

True

x = 0
while(x < 5):
print("The value of x is", str(x))
x = x + 1
print("x = " + str(x))

The value of x is 0
The value of x is 1
The value of x is 2
The value of x is 3
The value of x is 4
x = 5

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 3 of 10
Corey_Python_1.ipynb - Colab 10/05/24, 6:35 PM

greeting = 'Hello'
index = 0
while index < len(greeting):
print(greeting[index:index+2])
index += 1

He
el
ll
lo
o

def format_phone(phonenum):
area_code = "(" + phonenum[:3] + ")"
exchange = phonenum[3:6]
line = phonenum[-4:]
return area_code + " " + exchange + "-" + line

print(format_phone("2025551212")) # Outputs: (202) 555-1212

(202) 555-1212

def even_numbers(n):
count = 0
current_number = 0
while current_number <= n: # Complete the while loop condition
if current_number % 2 == 0:
count += 1 # Increment the appropriate variable
current_number += 1 # Increment the appropriate variable
return count

print(even_numbers(25)) # Should print 13


print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1

13
73
501
1

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 4 of 10
Corey_Python_1.ipynb - Colab 10/05/24, 6:35 PM

def even_numbers(n):
count = 0
current_number = 0
while current_number <= n: # Complete the while loop condition
if current_number % 2 == 0:
count += 1 # Increment the appropriate variable
current_number += 1 # Increment the appropriate variable
return count

print(even_numbers(25)) # Should print 13


print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1

13
73
501
1

def counter(start, stop):


if start > stop:
return_string = "Counting down: "
while start >= stop: # Complete the while loop
return_string += str(start) # Add the numbers to the "return_string"
if start > stop:
return_string += ","
start -= 1 # Increment the appropriate variable
else:
return_string = "Counting up: "
while start <= stop: # Complete the while loop
return_string += str(start) # Add the numbers to the "return_string"
if start < stop:
return_string += ","
start += 1 # Increment the appropriate variable
return return_string

print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"


print(counter(2, 1)) # Should be "Counting down: 2,1"
print(counter(5, 5)) # Should be "Counting up: 5"

Counting up: 1,2,3,4,5,6,7,8,9,10


Counting down: 2,1
Counting up: 5

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 5 of 10
Corey_Python_1.ipynb - Colab 10/05/24, 6:35 PM

for sum in range(5):


sum += sum
print(sum)

0
2
4
6
8

pets="Cats & Dogs"


pets.index("&")

pets="Cats & Dogs"


# pets.index("&")
# pets.index("C")
pets.index("Dog")
# pets.index("s")

def replace_domain(email, old_domain, new_domain):


if "@" + old_domain in email:
index = email.index("@" + old_domain)
new_email = email[:index] + "@" + new_domain
return new_email
return email

replace_domain("abc@bing.com", "gmail.com", "yahoo.com")

'abc@bing.com'

" test ".strip()

'test'

"Hello! How are you doing today?".split()

['Hello!', 'How', 'are', 'you', 'doing', 'today?']

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 6 of 10
Corey_Python_1.ipynb - Colab 10/05/24, 6:35 PM

"Hello! How are you doing today?".split('!')

['Hello', ' How are you doing today?']

"Hello, Nice Meeting you, How are you doing today?".split(',')

['Hello', ' Nice Meeting you', ' How are you doing today?']

name = "Sam"
number = 9876543210
salary = 50000
print("Hi {}, Thanks for sharing your number {}. Your salary is ${:>10.2f}".format(

Hi Sam, Thanks for sharing your number 9876543210. Your salary is $ 50000.00

name = "Sam"
number = 9876543210
salary = 50000
print(f"Hi {name}, Thanks for sharing your number {number}. Your salary is ${salary

Hi Sam, Thanks for sharing your number 9876543210. Your salary is $ 50000.00

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 7 of 10
Corey_Python_1.ipynb - Colab 10/05/24, 6:35 PM

def is_palindrome(input_string):
# Two variables are initialized as string date types using empty
# quotes: "reverse_string" to hold the "input_string" in reverse
# order and "new_string" to hold the "input_string" minus the
# spaces between words, if any are found.
new_string = ""
reverse_string = ""

# Complete the for loop to iterate through each letter of the


# "input_string"
for letter in input_string:

# The if-statement checks if the "letter" is not a space.


if letter != " ":

# If True, add the "letter" to the end of "new_string" and


# to the front of "reverse_string". If False (if a space
# is detected), no action is needed. Exit the if-block.
new_string = new_string + letter
reverse_string = letter + reverse_string

# Complete the if-statement to compare the "new_string" to the


# "reverse_string". Remember that Python is case-sensitive when
# creating the string comparison code.
print(new_string)
print(reverse_string)

if new_string.lower() == reverse_string.lower():

# If True, the "input_string" contains a palindrome.


return True

# Otherwise, return False.


return False

print(is_palindrome("Never Odd or Even")) # Should be True


# print(is_palindrome("abc")) # Should be False
# print(is_palindrome("kayak")) # Should be True

NeverOddorEven
nevEroddOreveN
True

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 8 of 10
Corey_Python_1.ipynb - Colab 10/05/24, 6:35 PM

colors = ['Red', 'green', 'blue', 'orange']


colors.append('violet')
colors.insert(1, 'white')
colors.remove('blue')
colors
colors.pop(2)
colors

['Red', 'white', 'orange', 'violet']

words = []
word = "hello"
a = len(word)
word

def count_letters(text):
result = {}
for letter in text:
if letter not in result:
result[letter] = 0
result[letter] += 1
return result
count_letters("aaaaa")

{'a': 5}

Start coding or generate with AI.

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 9 of 10
Corey_Python_1.ipynb - Colab 10/05/24, 6:35 PM

https://colab.research.google.com/drive/14subOArsgsekUJVEcCTaYSUz0kq0s4Ix Page 10 of 10

You might also like