My First Blog _Python

WBOY
Release: 2024-07-16 22:31:32
Original
938 people have browsed it

My First Blog _Python

Hi All,

This is my very first blog. I'm a Biomath student who has zero knowledge about computer related stuffs.
Later, I was convinced to study a programming language yet confused with how,where and what to start...?

And finally decided to learn python, as people around me implied that it's easy to start with... So, now I'm learning python from an online platform., which teaches me so fine...

I thought of blogging it all, cause people like me will get to know that python isn't overly challenging rather as simple as English grammar.

So, join with me...
Let's start to learn PYTHON

PYTHON BASICS

1. Printing a String :

  • To print a text, we use print function - print()

  • Let's begin with "Hello World".

  • By writing certain text within the print function inside double or single quotes, we get :

print("Hello World")

HelloWorld

Copy after login

2. Similarly, we can Print Variables :

  • Variables are used to store data.
name = "Abys"
print(name)

Abys

Copy after login
  • Here, if we give numbers as values for the variable (ex: age=25) we needn't provide them under double or single quotes as they give the same output but as for texts we should use "" or ''.
age = 25 
print(age)

25

age = "25"
print(age)

25

name = Abys
print(name)

Traceback (most recent call last):
  File "./prog.py", line 4, in <module>
NameError: name 'Abys' is not defined

Copy after login
  • That's the reason for this rule. Hope it's clear.,

3. Printing Multiple Items :

  • We can print multiple items by separating them with commas while python adds space between each item.

  • Formatting strings with f strings is another sub topic where we insert variables directly into the string by prefixing it with an f and using curly braces {} around the variables.

let's see both,

name="Abys"
age=17
city="Madurai"
print("Name:",name , "Age:",age , "City:",city ,end=".")

Name: Abys Age: 17 City: Madurai.

print(f"Name:{name},Age:{age},City:{city}.")

Name:Abys,Age:17,City:Madurai.

Copy after login

4.Concatenation of Strings :

  • Here, we connect words using + operator.
w1="Sambar"
w2="Vada"
print(w1+" "+w2+"!")

Sambar Vada!

Copy after login
  • Let's also see what is Printing Quotes inside Strings.

  • To print quotes inside a string, you can use either single or double quotes to enclose the string and the other type of quotes inside it.

w1="Sambar"
w2="Vada"
print("I love" , w1+" "+w2+"!")

I love Sambar Vada!

hobby = "Singing"
print("My hobby is" , hobby)

My hobby is Singing

Copy after login

5.Escape Sequences and Raw Strings to Ignore Escape Sequences :

  • Escape sequences allows to include special characters in a string. For example, n adds a new line.
print("line1\nline2\nline3")

line1
line2
line3

Copy after login
  • r string is used as prefix which treats backslashes as literal characters.
print(r"C:\Users\Name")

C:\Users\Name

Copy after login

6.Printing Results of Mathematical Expressions :

  • We've already seen how to print numbers.
print(26)

26

Copy after login
  • Now, we're going to print certain eqns.
print(5+5)

10

print(3-1)

2

Copy after login

that's how simple it is...

7.Printing Lists and Dictionaries :

  • We can print entire lists and dictionaries.
fruits = ["apple", "banana", "cherry"]
print(fruits)

['apple', 'banana', 'cherry']

Copy after login

8.Using sep and end Parameters :

  • The sep parameter changes the separator between items.

  • The end parameter changes the ending character.

print("Happy", "Holiday's", sep="-", end="!")

Happy-Holiday's!

Copy after login
  • Let's see how to print the same in adjacent lines, here we use either escape sequences (n) or Multiline Strings.

  • Triple quotes allow you to print multiline strings easily.

print("""Happy
Holiday's""")

print("Happy\nHoliday's")

Copy after login

both gives same output as :

Happy
Holiday's

Copy after login

9.Combining Strings and Variables :

  • Combining strings and variables by using + for simple cases or formatted strings for more complex scenarios.

  • For simple case:

colour = "purple"
print("The colour of the bag is "+colour)

The colour of the bag is purple

Copy after login
  • For complex case :
temperature=22.5
print("The temperature is", str(temperature),"degree Celsius",end=".")

The temperature is 22.5 degree Celsius.

Copy after login

10.Printing with .format() and Using print for Debugging:

  • Use the .format() method for string formatting.
name="Abys"
age=17
city="Madurai"
print("Name:{}, Age:{}, City:{}".format(name,age,city))

Name:Abys, Age:17, City:Madurai

Copy after login
  • We can use print to debug your code by printing variable values at different points.
def add(a, b):
    print(f"Adding {a} and {b}")
    return a + b

result = add(1, 2)
print("Result:", result)

Adding 1 and 2
Result: 3

Copy after login

That's it.
These were the topics I learned in my 1st class.

At the beginning, I was confused by all the terms but as time went I got used to it just like I we first started to learn English.

Try it out yourself... as u begin to get the output., it's next level feeling.
I personally felt this;
"Nammalum Oru Aal Than Pola"...

.....

The above is the detailed content of My First Blog _Python. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!